Preface
The general implementation interface is to write the implementation class by yourself
public interface MyDao{
public void save();
}
//编写实现类文件
public class MyDaoClass implements MyDao{
public void save(){
System.out.println("Hello");
}
}
You can also use dynamic agents to help you generate implementation classes in the memory when the program is running.
For example, the **getMapper()** method can help us generate the implementation class.
1. Preparation
1. Write the persistence layer interface: UserDao interface
src\main\java\cn\cyl\dao\UserDao.java
public interface UserDao {
}
2. Write the mapping configuration of the persistence layer interface: UserDao.xml
src\main\resources\cn\cyl\dao\UserDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--mapper的约束文件-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace要求写接口的全类名:包名+类名=全类名-->
<mapper namespace="cn.cyl.dao.UserDao">
</mapper>
3. Configure the mapping in the core configuration file SqlMapConfg.xml file
src\main\resources\SqlMapConfig.xml
<!--配置多个映射文件,告知mybatis映射配置的位置-->
<mappers>
<mapper resource="UserMapper.xml"/>
<mapper resource="cn/cyl/dao/UserDao.xml"/>
</mappers>
4. User entity class, user table
src\main\java\cn\cyl\bean\User.java
public class User {
private int id;
private String username;
private Date birthday;
private int sex;
private String address;
//省略get set toString方法
}
note:
- The mapping configuration of the persistence layer interface and the persistence layer interface must be in the same package
- The value of the namespace attribute of the mapper tag in the persistence layer mapping configuration must be the fully qualified class of the persistence layer interface .
- The id attribute of the configuration tags <select>,<insert>,<delete>,<update> of the SQL statement must be the same as the method name of the persistence layer interface .
Second, query by id
1. Add a method to the UserDao interface
public interface UserDao {
User findById(int i);
}
2. UserDao.xml mapping file configuration <select> tag
<mapper namespace="cn.cyl.dao.UserDao">
<!--使用代理以后,id必须是方法名-->
<select id="findById" parameterType="int" resultType="cn.cyl.bean.User">
select * from user where id = #{id};
</select>
</mapper>
3. Add query test method
@Test
public void selectTest01(){
//创建SqlSession对象
SqlSession sqlSession = MySessionUtils.getSession();
//getMapper()方法:对你编写的接口使用动态代理技术实现,直接返回实现类对象,即代理对象
//代理对象的方法内部就是查询sql,并且执行jdbc代码
//参数:class<T> aClass
UserDao dao = sqlSession.getMapper(UserDao.class);
//定义变量接收
User user = dao.findById(1);
//打印结果
System.out.println(user);
//释放资源
sqlSession.close();
}
4. Console output
3. Fuzzy query based on user name
1. Add a method to the UserDao interface
List<User> findByKeyword(String s);
2. UserDao.xml mapping file configuration <select> tag
<select id="findByKeyword" parameterType="string" resultType="User">
select * from user where username like #{keyword}
</select>
3. Add query test method
@Test
public void selectTest02(){
//创建SqlSession对象
SqlSession sqlSession = MySessionUtils.getSession();
//返回接口的实现类对象
UserDao dao = sqlSession.getMapper(UserDao.class);
//搜索姓张的人
List<User> list = dao.findByKeyword("张%");
for (User user:list){
System.out.println(user);
}
sqlSession.close();
}
4. Console output
Fourth, save users
1. Add a method to the UserDao interface
void saveUser(User user);
2. UserDao.xml mapping file configuration <insert> tag
<insert id="saveUser" parameterType="user">
insert into user values(null,#{username},#{birthday},#{sex},#{address})
</insert>
3. Add the inserted test method
@Test
public void insertTest04(){
//创建SqlSession对象
SqlSession sqlSession = MySessionUtils.getSession();
UserDao dao = sqlSession.getMapper(UserDao.class);
//定义接口
User user = new User();
// user.setId(101);
user.setUsername("jack");
user.setAddress("北京");
user.setBirthday(new Date());
//执行存储方法
dao.saveUser(user);
//提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
4. Console output
Five, delete users based on id
1. Add a method to the UserDao interface
void deleteById(int i);
2. UserDao.xml mapping file configuration <delete> tag
<delete id="deleteById" parameterType="int">
delete from user where id = #{id}
</delete>
3. Add and delete test methods
@Test
public void deleteTest03(){
//创建SqlSession对象
SqlSession sqlSession = MySessionUtils.getSession();
//返回接口的实现类对象
UserDao dao = sqlSession.getMapper(UserDao.class);
//删除id为104的用户
dao.deleteById(104);
//提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
4. Console output