Mapper代理接口(动态代理,有接口的情况,mybatis自动是生成实现类)
只需要写Mapper接口,相当于dao接口
1) 需要编写mapper.xml, Mapper接接口,Mapper接口需要遵循一些开发规范,Mybatis自动生成Mapper接口实现类的代理对象
2)4个开发规范
a)mapper.xml中namespace要等于接口的地址(全路径,包.类名)
b)mapper接口中的方法名要与mapper.xml文件中statement的id名称相同
c)mapper接口中方法的输入参数类型要与mapper.xml文件中statement的输入类型相同
d) mapper接口中方法的返回值类型要与mapper.xml文件中语句标签的返回值类型相同
Mapper接口:
/**
* User的Mapper接口
* @author Administrator
*
*/
public interface UserMapper {
//根据id查找用户
public User findUserById(int id) throws Exception;
//添加用户
public void insertUser(User user);
查询所有用户
public List<User> findAllUser();
//根据id删除用户
public void deleteUser(int id);
//修改用户昵称
public void updateNickname(User user);
//查询姓x的用户的个数
public int selectCount(String name);
}
mapper.xml:
<!-- mapper标签需要指定namespace属性,项目中属性值唯一 -->
<mapper namespace="com.mybatis.mapper.inter.UserMapper">
<!-- 根据id查找用户 -->
<!-- id:代表当前文件中查询的id,id在当前文件中唯一;
parameterType:用于指定输入参数的类型,可不写;
resultType:用于指定返回参数的类型,可不写 -->
<select id="findUserById" parameterType="int" resultType="com.mybatis.entity.User">
select * from user where id = #{id}
</select>
<!-- 查询所有用户 -->
<select id="findAllUser" resultType="com.mybatis.entity.User">
select * from user
</select>
<!-- 添加用户 -->
<insert id="insertUser" parameterType="com.mybatis.entity.User">
<!-- 将插入的主键返回,返回到输入参数的对象中 -->
<!-- keyProperty:将查询到的主键值设置到parameterType指定的对象的那个属性
order:是相对于insert语句来说它的执行顺序
resultType:用于指定返回参数的类型 -->
<selectKey keyProperty="id" order="AFTER" resultType="int">
<!-- 获取刚刚插入的记录的主键 -->
select LAST_INSERT_ID()
</selectKey>
insert into user(name,age,nickname) values(#{name},#{age},#{nickname})
</insert>
<!-- 删除用户 -->
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
<!-- 配置用户昵称 -->
<update id="updateNickname" parameterType="com.mybatis.entity.User">
update user set nickname=#{nickname} where id=#{id}
</update>
<!-- /查询姓x的用户的个数 -->
<select id="selectCount" parameterType="java.lang.String" resultType="int">
<!-- 解析时候,name传入李,#会自动加上单引号'李'
name' and pwd='123
$ 李 update set name=''-->
select count(id) from user where name like #{name}"%"
</select>
</mapper>
test.java:
public class test{
//SqlSessionFactory单例对象,一旦创建,不再销毁,一直使用这个实例
private SqlSessionFactory factory;
@Before//初始化方法,每个测试方法之前都要执行一次
public void before() throws Exception{
//1、得到配置文件流
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//2、创建会话工厂
factory = new SqlSessionFactoryBuilder().build(inputStream);
}
/*
* SqlSession最佳应用场合,是在方法体内,定义成一个局部变量,让每个对象都有自己的独立区域,多线程影响不到
*/
@org.junit.Test
public void testFindById() throws Exception{
//3、通过工厂得到会话
SqlSession session = factory.openSession();
//获取到Mapper接口
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.findUserById(6);
System.out.println(user);
//5、释放资源
session.close();
}
@org.junit.Test
public void testInsertUser() throws Exception{
//3、通过工厂得到会话
SqlSession session = factory.openSession();
//获取到Mapper接口
UserMapper mapper = session.getMapper(UserMapper.class);
User user = new User();
user.setName("礼拜");
user.setAge(20);
user.setNickname("jjfiejfe");
mapper.insertUser(user);
System.out.println(user);
session.commit();
//5、释放资源
session.close();
}
@org.junit.Test
public void testFindAllUser() throws Exception{
//3、通过工厂得到会话
SqlSession session = factory.openSession();
//获取到Mapper接口
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> list = mapper.findAllUser();
for (User user : list) {
System.out.println(user);
}
//5、释放资源
session.close();
}
@org.junit.Test
public void testDeleteUser() throws Exception{
//3、通过工厂得到会话
SqlSession session = factory.openSession();
//获取到Mapper接口
UserMapper mapper = session.getMapper(UserMapper.class);
mapper.deleteUser(14);
session.commit();
//5、释放资源
session.close();
}
@org.junit.Test
public void testUpdateNickname() throws Exception{
//3、通过工厂得到会话
SqlSession session = factory.openSession();
//获取到Mapper接口
UserMapper mapper = session.getMapper(UserMapper.class);
User user = new User();
user.setId(15);
user.setNickname("jack");
mapper.updateNickname(user);
System.out.println(user);
session.commit();
//5、释放资源
session.close();
}
@org.junit.Test
public void testUserCount() throws Exception{
//3、通过工厂得到会话
SqlSession session = factory.openSession();
//获取到Mapper接口
UserMapper mapper = session.getMapper(UserMapper.class);
int count = mapper.selectCount("王");
System.out.println(count);
//5、释放资源
session.close();
}
}