- 在pom.xml中配置
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
2.从xml中构建SqlSessionFactory
①配置xml,在src/main/resource下创建mybaties-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,代表唯一标识
<environment id="development">
//采用JDBC方式对数据库事物进行管理 commit/rollback
<transactionManager type="JDBC"/>
//对数据源的管理,POOLED代表使用基于连接池的方式进行连接
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisvip10?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
3.进行测试用例
①导入
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
②建一个junit的类进行测试用例
在test下面建一个MybatisDemo
4.针对SqlSessionFactory进行封装处理,要保证它全局唯一,(创建工具类)
在java.com.softeem.util下创建MybatisUtil类:
public class MybatisUtil {
private static SqlSessionFactory = null;
static {
try{
//加载配置文件
Reader reader = Resources.getResourceAsReader("mybaties-config.xml");
//利用构造者模式根据配置文件初始化sqlSessionFactory对象
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch (IOException e){
e.printStackTrace();
}
}
//创建SqlSesion对象
public static SqlSession createSession(){
return sqlSessionFactory.openSession();
}
//关闭SqlSession会话
public static void closeSession(SqlSession sqlSession){
sqlSession.close();
}
}
5.根据数据库建表,然后再com.softeem.entity下创建实体类,例如:Student, Teacher。。。
6.配置映射文件(核心)用来操作数据库(JDBC)
<?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="XXX">
<select id="queryAll" resultType="com.softeem.entity.Student">
select id,nickname,sex,moble,birthday
FROM tablename1
</select>
<!--parameterType:参数的配置-->
<!--#{}里面写什么都可以-->
<select id="queryById" parameterType="Integer" resultType="com.softeem.entity.Student">
select id,nickname,sex,moble,birthday
FROM tablename1 WHERE id= #{id}
</select>
<insert id="insertStudent" parameterType="com.softeem.entity.Student">
INSERT INTO tablename1(nickname,sex,moble,birthday)
VALUES(#{nickname},#{sex},#{moble})
</insert>
</mapper>
7.注册映射器
在mybatis-config里:
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
8.在测试类中进行测试
@Test
public void testSqlSessionfactory(){
SqlSession sqlSession = null;
try {
//加载配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//利用构造者模式根据配置文件的信息初始化SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder(). build(reader);
//sqlSessionFactory创建对象sqlSession对象
sqlSession = sqlSessionFactory.openSession();
Connection connection = sqlSession.getConnection();
System.out.println(connection);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession!=null){
sqlSession.close();
}
}
}
@Test
public void testMybatisUtil(){
SqlSession sqlSession = MybatisUtil.createSqlSession();
System.out.println(sqlSession.getConnection());
MybatisUtil.closeSession(sqlSession);
}
@Test
public void testQueryAll(){
SqlSession sqlSession =null;
try{
sqlSession = MybatisUtil.createSqlSession();
List<Student> list = sqlSession.selectList("XXX.queryAll");
for (Student student:list){
System.out.println(student);
}
}catch (Exception e){
e.printStackTrace();
}finally {
MybatisUtil.closeSession(sqlSession);
}
}
/*根据id查询单个*/
@Test
public void testQueryById(){
SqlSession sqlSession =null;
try{
sqlSession = MybatisUtil.createSqlSession();
Student student = sqlSession.selectOne("XXX.queryById",2);
System.out.println(student);
}catch (Exception e){
e.printStackTrace();
}finally {
MybatisUtil.closeSession(sqlSession);
}
}
/*增的操作*/
@Test
public void testInsertStudent(){
SqlSession sqlSession= null;
try{
sqlSession = MybatisUtil.createSqlSession();
Student student= new Student();
student.setNickname("湘湘");
student.setSex(1);
student.setMobile("123124214");
int insert = sqlSession.insert("XXX.insertStudent", student);
System.out.println("num"+insert);
}catch (Exception e){
e.printStackTrace();
}finally {
MybatisUtil.closeSession(sqlSession);
}
}