一、helloworld
Teacher ```java public class Teacher {
private Integer id; private String name; private String course; private String address; private Date birth; //省略get/set/toString/构造器 }
- TeacherDao
```java
public interface TeacherDao {
public Teacher getTeacherById(Integer id);
}
- sql ```xml SELECT * FROM t_teacher WHERE id =2

- TeacherDao.xml
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.achang.dao.TeacherDao">
<resultMap id="teacherMap" type="com.achang.bean.Teacher">
<id property="id" column="id"/>
<result property="name" column="teacherName"/>
<result property="course" column="class_name"/>
<result property="address" column="address "/>
<result property="birth" column="birth_date"/>
</resultMap>
<select id="getTeacherById" resultMap="teacherMap">
SELECT * FROM t_teacher WHERE id =#{id}
</select>
</mapper>
- mybatis_config.xml
```xml
- test
```java
public class DaoTest {
SqlSessionFactory factory;
@Before
public void initSqlSessionFactory() throws IOException {
String resource = "mybatis_config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test1(){
SqlSession sqlSession = factory.openSession();
Teacher teacherById;
try {
TeacherDao mapper = sqlSession.getMapper(TeacherDao.class);
teacherById = mapper.getTeacherById(2);
} finally {
sqlSession.close();
}
System.out.println(teacherById);
//Teacher{id=2, name='张飞', course='数学', address='鹿城区', birth=Wed Sep 16 00:00:00 CST 2020}
}
}
二、if标签
- TeacherDao.xml
因为在xml文件中编写,会有歧义,需要使用转义符还区别各种符号
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.achang.dao.TeacherDao">
<resultMap id="teacherMap" type="com.achang.bean.Teacher">
<id property="id" column="id"/>
<result property="name" column="teacherName"/>
<result property="course" column="class_name"/>
<result property="address" column="address "/>
<result property="birth" column="birth_date"/>
</resultMap>
<select id="getTeacherByCondition" resultMap="teacherMap">
SELECT * FROM t_teacher WHERE
<!--test属性:指定编写判断条件
id!=null:取出传入的javabean属性中id的值,判断其是否为空
-->
<if test="id!=null">
id > #{id} and
</if>
<if test="name!=null and !name.equals("") ">
teacherName like #{name} and
</if>
<if test="birth!=null">
birth_date < #{birth}
</if>
</select>
</mapper>
- TeacherDao
```java
public interface TeacherDao {
//找到满足传入teacher对象中设置值条件的集合
public List
getTeacherByCondition(Teacher teacher); }
- DaoTest
```java
public class DaoTest {
SqlSessionFactory factory;
@Before
public void initSqlSessionFactory() throws IOException {
String resource = "mybatis_config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test1(){
SqlSession sqlSession = factory.openSession();
Teacher teacherById;
try {
TeacherDao mapper = sqlSession.getMapper(TeacherDao.class);
Teacher teacher = new Teacher();
//给teacher对象属性set值,代表传入值,对应上面的不为null
teacher.setId(1);
teacher.setName("%a%");
teacher.setBirth(new Date());
List<Teacher> teacherByCondition = mapper.getTeacherByCondition(teacher);
for (Teacher teacher1:teacherByCondition){
System.out.println(teacher1);
//Teacher{id=3, name='tomcat', course='英语', address='瑞安县', birth=Fri Jun 30 00:00:00 CST 2017}
}
} finally {
sqlSession.close();
}
}
}
三、where标签
我们的查询语句就放在where标签内,每一个and放在前面
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.achang.dao.TeacherDao">
<resultMap id="teacherMap" type="com.achang.bean.Teacher">
<id property="id" column="id"/>
<result property="name" column="teacherName"/>
<result property="course" column="class_name"/>
<result property="address" column="address "/>
<result property="birth" column="birth_date"/>
</resultMap>
<select id="getTeacherByCondition" resultMap="teacherMap">
SELECT * FROM t_teacher
<!--where标签:可以帮我们去除掉前面的and-->
<where>
<if test="id!=null">
id > #{id}
</if>
<if test="name!=null and !name.equals("") ">
and teacherName like #{name}
</if>
<if test="birth!=null">
and birth_date < #{birth}
</if>
</where>
</select>
</mapper>
四、trim标签
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.achang.dao.TeacherDao">
<resultMap id="teacherMap" type="com.achang.bean.Teacher">
<id property="id" column="id"/>
<result property="name" column="teacherName"/>
<result property="course" column="class_name"/>
<result property="address" column="address "/>
<result property="birth" column="birth_date"/>
</resultMap>
<select id="getTeacherByCondition" resultMap="teacherMap">
SELECT * FROM t_teacher
<!--截取字符串
prefix属性:为下面的sql整体添加一个前缀
prefixOverrides属性:取出整体字符串钱多多余的字符
suffix:为整体添加一个后缀
suffixOverrides:后面哪个多了可以去掉
-->
<trim prefix="where" prefixOverrides="and">
<if test="id!=null">
id > #{id}
</if>
<if test="name!=null and !name.equals("") ">
and teacherName like #{name}
</if>
<if test="birth!=null">
and birth_date < #{birth}
</if>
</trim>
</select>
</mapper>
五、foreach遍历元素
- TeacherDao
```java
public interface TeacherDao {
public List
getTeacherByIdIn(@Param(“ids”) List ids); }
- TeacherDao.xml
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.achang.dao.TeacherDao">
<resultMap id="teacherMap" type="com.achang.bean.Teacher">
<id property="id" column="id"/>
<result property="name" column="teacherName"/>
<result property="course" column="class_name"/>
<result property="address" column="address "/>
<result property="birth" column="birth_date"/>
</resultMap>
<select id="getTeacherByIdIn" resultMap="teacherMap">
SELECT * FROM t_teacher WHERE id IN
<!--foreach遍历集合
collection属性:指定要遍历集合的key
close属性:以书什么结束
item属性:每次遍历的元素,命名任意,方便引用
index属性:
如果遍历的是一个list,指定变量保存的当前元素的索引
item为值
如果遍历的是一个map,指定变量保存的当前元素的key
item为value值
open属性:以什么开始
separator属性:每次遍历元素的分隔符
-->
<foreach collection="ids" close=")" item="id_item" open="(" separator=",">
#{id_item}
</foreach>
</select>
</mapper>
test ```java public class DaoTest { SqlSessionFactory factory; @Before public void initSqlSessionFactory() throws IOException {
String resource = "mybatis_config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); factory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test public void test1(){
SqlSession sqlSession = factory.openSession(); Teacher teacherById; try { TeacherDao mapper = sqlSession.getMapper(TeacherDao.class); List<Teacher> teacherByIdIn = mapper.getTeacherByIdIn(Arrays.asList(1, 2, 3, 4, 5)); for (Teacher teacher:teacherByIdIn){ System.out.println(teacher); //Teacher{id=1, name='admin', course='语文', address='龙湾区', birth=Thu Dec 17 00:00:00 CST 2020} //Teacher{id=2, name='张飞', course='数学', address='鹿城区', birth=Wed Sep 16 00:00:00 CST 2020} //Teacher{id=3, name='tomcat', course='英语', address='瑞安县', birth=Fri Jun 30 00:00:00 CST 2017} } } finally { sqlSession.close(); }
}
}
<a name="G2faJ"></a>
## 六、choose标签
when标签:设置情况;满足后其他情况跳过<br />otherwise标签:当所有都不情况都不满足时,就执行此标签<br />类似ifelse-else
- TeacherDao
```java
public interface TeacherDao {
public List<Teacher> getTeacherByConditionChoose(Teacher teacher);
}
TeacherDao.xml ```xml <?xml version=”1.0” encoding=”UTF-8” ?> <!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- test
```java
public class DaoTest {
SqlSessionFactory factory;
@Before
public void initSqlSessionFactory() throws IOException {
String resource = "mybatis_config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test1(){
SqlSession sqlSession = factory.openSession();
Teacher teacherById;
try {
TeacherDao mapper = sqlSession.getMapper(TeacherDao.class);
Teacher teacher = new Teacher();
teacher.setId(2);
teacher.setName("admin");
List<Teacher> teacherByConditionChoose = mapper.getTeacherByConditionChoose(teacher);
} finally {
sqlSession.close();
}
}
}
七、set标签
用于SQL语句中的update操作
- sql ```xml UPDATE t_teacher SET teacherName=?, class_name=?, address=?, birth_date=? WHERE id=?
- TeaherDao
```java
public interface TeacherDao {
public int updateTeacher(Teacher teacher);
}
TeaherDao.xml ```xml <?xml version=”1.0” encoding=”UTF-8” ?> <!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
UPDATE t_teacher teacherName=#{name}, class_name=#{course}, address=#{address}, birth_date=#{birth} id=#{id}
- test
```java
public class DaoTest {
SqlSessionFactory factory;
@Before
public void initSqlSessionFactory() throws IOException {
String resource = "mybatis_config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test1(){
//开始自动提交事务
SqlSession sqlSession = factory.openSession(true);
Teacher teacherById;
try {
TeacherDao mapper = sqlSession.getMapper(TeacherDao.class);
Teacher teacher = new Teacher();
teacher.setId(2);
teacher.setName("admin");
int i = mapper.updateTeacher(teacher);
System.out.println(i);
} finally {
sqlSession.close();
}
}
}
八、bind标签【不推荐】
- bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。比如:
后期更改匹配规则麻烦
例如:设置调用myLastName时前后默认添加%
九、OGNL表达式
- OGNL( Object Graph Navigation Language )对象图导航语言(类似级联属性)
- 这是一种强大的表达式语言,通过它可以非常方便的来操作对象属性。类似于我们的EL,SpEL等