使用Mapper代理的方式进行开发
Mapper开发的原理##
使用Mybatis的Mapper代理方式进行开发,使用该方式,只需要编写Mapper接口,不再需要编写实现类,由Mybatis框架通过接口定义来自动生成接口的动态代理对象。
Mapper代理开发要遵循的原则
1. Mapper.xml文件中的namespace要和Mapper接口的类路径相同。
2. Mapper接口中的方法名称要和Mapper.xml中定义的每个statement的id相同。
3. Mapper接口的输入参数类型要和Mapper.xml文件中定义的相对应sql的parameterType的类型相同。
4. Mapper接口的输出参数类型要和Mapper.xml文件中定义的相对应的sql的resultType的类型相同。
Mapper代理开发过程
1. 创建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">
<!--namespace:Mapper接口类的路径-->
<mapper namespace="com.jack.dao.UserMapper">
<!--根据id查询用户-->
<select id="findUserById" parameterType="int" resultType="User">
SELECT * FROM user WHERE id=#{id}
</select>
<!--根据用户名模糊查询用户信息-->
<select id="findUserByName" parameterType="String" resultType="User" >
SELECT * FROM user WHERE username LIKE '%${value}%'
</select>
<!--插入用户信息-->
<insert id="addUser" parameterType="User">
<selectKey keyProperty="id" order="AFTER" resultType="Integer">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO user(username,birthday,sex,address) VALUE (#{username},#{birthday},#{sex},#{address})
</insert>
<!--根据id修改用户信息-->
<update id="updateUserById" parameterType="User">
UPDATE user SET username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} WHERE id=#{id}
</update>
<!--根据id删除用户信息-->
<delete id="deleteUserById" parameterType="int">
DELETE FROM user WHERE id=#{value}
</delete>
</mapper>
2、编写UserMapper接口文件
public interface UserMapper {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
//根据用户名查询用户
public List<User> findUserByName(String name) throws Exception;
//插入用户信息
public int addUser(User user) throws Exception;
//根据id修改用户信息
public void updateUserById(User user) throws Exception;
//根据id删除用户信息
public void deleteUserById(int id) throws Exception;
}
3、编写测试代码
public class UserTest {
public SqlSessionFactory sessionFactory;
public SqlSession sqlSession;
public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
@Before
public void setUp() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void findUserById(){
try {
sqlSession = sessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findUserById(1);
System.out.println("查询成功,信息如下:");
if(user.getBirthday() != null){
simpleDateFormat.format(user.getBirthday());
}
System.out.println(user);
}catch (Exception e){
System.out.println("查询失败");
}finally {
sqlSession.close();
}
}
@Test
public void findUserByName(){
try {
sqlSession = sessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> list = userMapper.findUserByName("小明");
System.out.println("查询成功,信息如下:");
for (User user: list) {
if(user.getBirthday() != null){
simpleDateFormat.format(user.getBirthday());
}
System.out.println(user);
}
}catch (Exception e){
System.out.println("查询失败");
}finally {
sqlSession.close();
}
}
@Test
public void addUser(){
try {
sqlSession = sessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUsername("小布");
user.setBirthday(new Date());
user.setSex("1");
user.setAddress("湖北 十堰");
int id = userMapper.addUser(user);
System.out.println("插入成功,新插入的用户id为:"+user.getId());
sqlSession.commit();
}catch (Exception e){
System.out.println("插入失败");
}finally {
sqlSession.close();
}
}
@Test
public void updateUserById(){
try {
sqlSession = sessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setId(4);
user.setUsername("小布");
user.setBirthday(new Date());
user.setSex("0");
user.setAddress("湖北 十堰");
userMapper.updateUserById(user);
sqlSession.commit();
System.out.println("id为"+user.getId()+"的用户信息更新成功");
}catch (Exception e){
System.out.println("更新失败");
}finally {
sqlSession.close();
}
}
@Test
public void deleteUserById(){
try {
sqlSession = sessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUserById(3);
sqlSession.commit();
System.out.println("删除成功");
}catch (Exception e){
System.out.println("更新失败");
}finally {
sqlSession.close();
}
}