示例:测试select、insert、update和delete操作

1.Mybatis-config
<?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>
<properties resource="db185.properties"></properties>
<!-- 指定MyBatis所用日志的具体实现 -->
<settings>
    <setting name="logImpl" value="LOG4J" />
</settings>
<!-- 环境配置,即链接的数据库 -->
<environments default="mysql">
    <environment id="mysql">
        <!-- 指定事务管理类型,type="JDBC"指直接简单使用JDBC的提交和回滚设置 -->
        <transactionManager type="JDBC"></transactionManager>
        <!-- dateSource指数据源配置,POOLED是JDBC链接对象的数据源连接池的实现 -->
        <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}" />
            <property name="password" value="${password}" />
        </dataSource>
    </environment>
</environments>
<!-- mappers告诉MyBatis去哪里找持久化类的映射文件 -->
<mappers>
    <mapper resource="org/fkit/mapper/UserMapper.xml" />
</mappers>
</configuration>
<properties resource=”db185.properties”></properties>配置表示引入db185.properties资源配置文件,以替换${driver}等变量
2.UserMapper
<?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="org.fkit.mapper.UserMapper">
    <insert id="saveUser" parameterType="org.fkit.domain.User" useGeneratedKeys="true">
        INSERT INTO tb_user(NAME,SEX,AGE) VALUES(#{name},#{sex},#{age})
    </insert>
    <select id="selectUser" parameterType="int" resultType="org.fkit.domain.User">
    SELECT * FROM tb_user where id=#{id}
    </select>
    <update id="modifyUser" parameterType="org.fkit.domain.User">
    UPDATE tb_user SET name=#{name},sex=#{sex},age=#{age}
    WHERE id=#{id}
    </update>
    <delete id="removeUser" parameterType="int">
    DELETE FROM tb_user WHERE id=#{id}
    </delete>
</mapper>
在UserMapper中定义了insert,update,select,delete4个元素,分别对应CURD四种数据库基本操作。
因为每次测试都需要读取Mybatis的配置文件,根据根配置文件信息创建SqlSessionFactory对象,再获取SqlSession对象,使得操作比较繁琐,所以开发一个工厂类封装以上操作的重复代码:
3.工厂类
package org.fkit.factory;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class FKSqlSessionFactory
{
private static SqlSessionFactory sqlSessionFactory = null;
//初始化创建SqlSessonFactory对象
static
{
try (
//读取Mybatis-config.xml
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
)
{
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (Exception e)
{
e.printStackTrace();
}
}
//获取SqlSession对象的静态方法
public static SqlSession getSqlSession()
{
return sqlSessionFactory.openSession();
}
//获取SqlSessionFactory对象的静态方法
public static SqlSessionFactory getSqlSessionFactory()
{
return sqlSessionFactory;
}
}
4.增
@GetMapping("/testInsert")
public String testInsert()
{
SqlSession sqlSession = FKSqlSessionFactory.getSqlSession();
User user = new User("bbb","m",22);
sqlSession.insert("org.fkit.mapper.UserMapper.saveUser",user);
sqlSession.commit();
sqlSession.close();
return "1";
}

5.查
@GetMapping("/testSelect")
public void testSelect()
{
SqlSession sqlSession = FKSqlSessionFactory.getSqlSession();
User user = sqlSession.selectOne("org.fkit.mapper.UserMapper.selectUser",1);
sqlSession.close();
System.out.println(user.getName());

6.改
@GetMapping("/testUpdate")
public void testUpdate()
{
SqlSession sqlSession = FKSqlSessionFactory.getSqlSession();
User user = sqlSession.selectOne("org.fkit.mapper.UserMapper.selectUser",1);
user.setAge(25);
sqlSession.update("org.fkit.mapper.UserMapper.modifyUser",user);
sqlSession.commit();
sqlSession.close();
}

7.删
@GetMapping("testDelete")
public void testDelete()
{
SqlSession sqlSession = FKSqlSessionFactory.getSqlSession();
sqlSession.delete("org.fkit.mapper.UserMapper.removeUser",1);
sqlSession.commit();
sqlSession.close();
}