Mybatis使用

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development"> <!-- 设置环境 -->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${驱动类(含包名)}"/>
<property name="url" value="${数据库连接URL}"/>
<property name="username" value="${用户名}"/>
<property name="password" value="${密码}"/>
</dataSource>
</environment>
</environments>
</configuration>

Util

一般只需要创建一次,所以创建一个工具类

public class MybatisUtil {

//在类加载时就进行创建
private static SqlSessionFactory sqlSessionFactory;
static {
try {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(new FileInputStream("mybatis-config.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

/**
* 获取一个新的会话
* @param autoCommit 是否开启自动提交(跟JDBC是一样的,如果不自动提交,则会变成事务操作)
* @return SqlSession对象
*/
public static SqlSession getSession(boolean autoCommit){
return sqlSessionFactory.openSession(autoCommit);
}
}

在其他函数里直接使用

try (SqlSession session = MybatisUtil.getSession(true)) {
session.selectOne("id", argu);
}

Mapper

原始方法

  1. mybatis-config.xml中添加
<mappers>
<mapper url="file:mappers/TestMapper.xml"/>
<!-- 这里用的是url,也可以使用其他类型,我们会在后面讲解 -->
</mappers>
  1. 定义TestMapper.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.test.mapper.TestMapper"> <!-- 命名空间指明mapper接口名 -->
<select id="selectStudent" resultType="com.test.entity.Student">
<!-- id:函数名;resultType:返回类型 -->
select * from student
</select>
<select id="selectOneStudent" resultType="com.test.entity.Student">
<!-- id:函数名;resultType:返回类型 -->
select * from student where sid = #{sid}
</select>
</mapper>

使用

public static void main(String[] args) throws FileNotFoundException {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(new FileInputStream("mybatis-config.xml")); // config 文件路径
try (SqlSession sqlSession = sqlSessionFactory.openSession(true)){
List<Student> student = sqlSession.selectList("selectStudent"); // xml id
Student stu3 = sqlSession.selectOne("selectOneStudent", 3); // 传参
student.forEach(System.out::println);
}
}

接口方法

  1. mybatis-config.xml中添加
<mappers>
<mapper resource="com/test/mapper/TestMapper.xml"/>
</mappers>
  1. 定义TestMapper.xml和接口TestMapper.java
public interface TestMapper {
List<Student> selectStudent();
}

使用

public static void main(String[] args) {
try (SqlSession sqlSession = MybatisUtil.getSession(true)){
TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
List<Student> student = testMapper.selectStudent(); // 已经定义好了类型
student.forEach(System.out::println);
}
}

注解方法

  1. mybatis-config.xml中添加
<mappers>
<mapper class="com.test.mapper.TestMapper"/>
// 单个java class
</mappers>

或者

<mappers>
<mapper package="com.test.mapper"/>
// 整个包
</mappers>

使用

public interface TestMapper {
@Results({
@Result(id = true, column = "id", property = "sid",
one = @One(select = "method")),
})
@Select("select * from student") // 注解
List<Student> selectStudent();

@Select("select * from student where sid = #{s}, bid = #{book.bid}")
Student selectStudentBySid(@Param("s") int sid, @Param("book") Book book) // 标明变量

Type method();
}

动态SQL

if

<if test="argu == value">
// your sql query.
</if>