Mybatis的CRUD
conf.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>
<!-- 设置别名 -->
<typeAliases>
<typeAlias type="entity.User" alias="user" />
</typeAliases>
<environments default="mybatisDemo">
<environment id="mybatisDemo">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="username" value="root" />
<property name="password" value="" />
<property name="url" value="jdbc:mysql:///test" />
</dataSource>
</environment>
</environments>
<!-- 注册userMapper.xml文件 -->
<mappers>
<mapper resource="mapper/userMapper.xml" />
</mappers>
</configuration>
userMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 -->
<mapper namespace="mapper.userMapper">
<!-- resultMap 解决实体类和表字段冲突 -->
<resultMap type="user" id="userResultMap">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="pwd" column="password" />
</resultMap>
<!-- 根据id查询user对象,参数类型为int -->
<select id="getUser" parameterType="int" resultMap="userResultMap">
select * from t_user where id=#{id}
</select>
<!-- 新增用户 -->
<insert id="addUser" parameterType="user">
insert into
t_user(id,name,password)values(#{id},#{name},#{pwd})
</insert>
<!-- 修改用户信息 -->
<update id="updateUser" parameterType="user">
update t_user set
name=#{name},password=#{pwd} where id=#{id}
</update>
<!-- 删除用户 -->
<delete id="deleteUser" parameterType="int">
delete from t_user where
id=#{id}
</delete>
</mapper>
MybatisTest文件
package test;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import entity.User;
import util.MybatisUtil;
public class MybatisTest {
@Test
public void addUser() {
User user = new User();
user.setId(5);
user.setName("AA");
user.setPwd("123123");
SqlSession sqlSession = MybatisUtil.getSqlSession();
sqlSession.insert("mapper.userMapper.addUser", user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void selectUser() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
User user = sqlSession.selectOne("mapper.userMapper.getUser", 1);
System.out.println(user);
}
@Test
public void updateUser() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
User user = new User();
user.setId(5);
user.setName("BB");
user.setPwd("123123");
sqlSession.update("mapper.userMapper.updateUser", user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
User user = new User();
user.setId(5);
sqlSession.update("mapper.userMapper.deleteUser", user);
sqlSession.commit();
sqlSession.close();
}
}
MybatisUtil工具类
package util;
import java.io.IOException;
import java.io.Reader;
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 MybatisUtil {
public static SqlSession sqlSession;
public static SqlSession getSqlSession() {
try {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSession = sessionFactory.openSession();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sqlSession;
}
}