一、Mapper代理开发方式(非常重要!!!)
思路:
Mapper代理的开发方式,我们只需要写出dao层的接口即可,Mybatis会自动为接口生成动态代理实现类。(不过要实现mapper代理的开发方式,需要遵循一些开发规范)
开发规范:
- mapper接口的权限定类名要与mapper映射文件 的namesapce的值相同。
- mapper接口的方法名要和mapper映射文件中statment的 id相同。
- mapper接口的返回值类型要与mapper映射文件中的statment的ResultType的值或者ResultMap中的type值保持一致。
- mapper接口的参数只能是一个并且要与mapper映射文件中的statment的parameterType的值保持一致。
Mapper接口
public interface UserMapper {
//通过id查询用户
UsersBean selUserById(int id);
//通过名称模糊查询用户
List<UsersBean> selUserByName(String userName);
//添加用户
void addUser(UsersBean user);
//删除用户
void delUser(String userName);
//修改用户
void updateUser(UsersWapper usersWapper);
//通过id查询用户
UsersWapper selUserBywapper(int id);
}
Mapper.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.asjy.dao.UserMapper">
<!--result配置节 -->
<resultMap type="UsersWapper" id="aaa">
<association property="user" javaType="UsersBean">
<!-- column是数据库字段 property是po类字段 -->
<id column="id" property="id"/>
<result column="userName" property="userName"/>
</association>
</resultMap>
<select id="selUserById" parameterType="int" resultType="UsersBean">
select *
from tb_users
where id = #{id}
</select>
<!-- 2、 根据用户名称来模糊查询用户信息列表;-->
<select id="selUserByName" parameterType="String" resultType="UsersBean">
select *
from tb_users
where userName like "%${value}%"
</select>
<!-- 3、 添加用户-->
<insert id="addUser" parameterType="UsersBean">
<selectKey keyProperty="id" resultType="int" order="AFTER" >
select LAST_INSERT_ID()
</selectKey>
insert into tb_users
values(default,#{depID},#{userName},#{userPwd},#{userCode},#{userSex},#{userAge},#{userPower})
</insert>
<!-- 4、 删除用户(练习)-->
<delete id="delUser" parameterType="String">
delete from tb_users
where userName = #{userName}
</delete>
<!-- 5、 修改用户(练习) -->
<update id="updateUser" parameterType="UsersWapper">
update tb_users
set userPower = #{user.userPower}
where userName = #{user.userName}
</update>
<select id="selUserBywapper" resultMap="aaa">
select *
from tb_users
where id = #{id}
</select>
</mapper>
测试代码
@Test
public void fun1(){//读取配置文件
SqlSession openSession = DBUtil.getSession();
UserMapper user = openSession.getMapper(UserMapper.class);
UsersBean selUserById = user.selUserById(100942);
System.out.println(selUserById);
}
@Test
public void fun2(){
SqlSession openSession = DBUtil.getSession();
UserMapper user = openSession.getMapper(UserMapper.class);
List<UsersBean> name = user.selUserByName("a");
for (UsersBean usersBean : name) {
System.out.println(usersBean);
}
}
@Test
public void fun3(){
SqlSession openSession = DBUtil.getSession();
UserMapper userMapper = openSession.getMapper(UserMapper.class);
UsersBean user = new UsersBean();
user.setDepID(2);
user.setUserAge(22);
user.setUserCode("10001");
user.setUserName("丽丽");
user.setUserPower(2);
user.setUserPwd("123456");
user.setUserSex("男");
userMapper.addUser(user);
System.out.println("id::"+user.getId());
openSession.commit();
}