Fundamentals of mybatis framework for java learning
0x00 Preface
Continue the introduction of the previous article and continue the content of mark mybatis. The previous chapter only wrote a simple query function of mybatis. This article is to write his deletion, modification, query and other operations.
0x01 mybatis add Dharma
In fact, there is little difference between adding and querying. Modifying the mapping file and then modifying it on the basis of query becomes an added function. See the code for details:
<?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.test.dao.Userdao">
<select id="findAll" resultType="com.test.domain.User">
select * from user
</select>
<insert id="SaveUser" parameterType="com.test.domain.User">
insert into user(name,address,gender,age,qq,email,username,password) values(#{name},#{address},#{gender},#{age},#{qq},#{email},#{username},#{password})
</insert >
</mapper>
Under the select tag, add an insert tag.
Test class:
package com.test;
import com.test.dao.Userdao;
import com.test.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.sqlSession;
import org.apache.ibatis.session.sqlSessionFactory;
import org.apache.ibatis.session.sqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class mybatistest {
@Test
public void selecttest()throws Exception{
// public static void main(String[] args) {
//1.读取配置文件
InputStream in = Resources.getResourceAsStream("mapconfig.xml");
//2.创建sqlSessionFactory工厂
sqlSessionFactoryBuilder builder = new sqlSessionFactoryBuilder();
sqlSessionFactory factory = builder.build(in);
//3.使用工厂生产sqlSession对象
sqlSession session = factory.openSession();
//4.使用sqlSession创建Dao接口的代理对象
Userdao userDao = session.getMapper(Userdao.class);
//5.使用代理对象执行方法
List<User> users = userDao.findAll();
for(User user : users){
System.out.println(user.toString());
}
//6.释放资源
session.close();
in.close();
}
@Test
public void Savetest() throws IOException {
User user = new User();
user.setName("nice");
user.setAge(20);
user.setGender("男");
user.setAddress("gd");
user.setQq("1402773304");
user.setEmail("1402773304@qq.com");
user.setUsername("nice0e3");
user.setPassword("nize");
InputStream in = Resources.getResourceAsStream("mapconfig.xml");
//2.创建sqlSessionFactory工厂
sqlSessionFactoryBuilder builder = new sqlSessionFactoryBuilder();
sqlSessionFactory factory = builder.build(in);
//3.使用工厂生产sqlSession对象
sqlSession session = factory.openSession();
//4.使用sqlSession创建Dao接口的代理对象
Userdao userDao = session.getMapper(Userdao.class);
userDao.SaveUser(user); //调用SaveUser方法参数user对象
session.commit();//提交事务
}
}
There are two methods, one is query and the other is add. Although the function is completed, we can see that some of the previous codes are repeated. We can directly encapsulate the repeated codes and call them directly when we want to execute a function.
package com.test;
import com.test.dao.Userdao;
import com.test.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.sqlSession;
import org.apache.ibatis.session.sqlSessionFactory;
import org.apache.ibatis.session.sqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class mybatistest {
private InputStream in;
private sqlSessionFactoryBuilder builder;
private Userdao userDao;
private sqlSession session;
private sqlSessionFactory factory;
private User user = new User();
@Before //被注解后在测试方法执行前会执行
public void init() throws IOException {
in = Resources.getResourceAsStream("mapconfig.xml");
builder = new sqlSessionFactoryBuilder();
factory = builder.build(in);
session = factory.openSession();
session.commit();//提交事务
userDao = session.getMapper(Userdao.class);
}
@Test
public void selecttest()throws Exception{
// public static void main(String[] args) {
//1.读取配置文件
//5.使用代理对象执行方法
List<User> users = userDao.findAll();
for(User user : users){
System.out.println(user.toString());
}
//6.释放资源
}
@Test
public void Savetest() throws IOException {
user.setName("nice");
user.setAge(20);
user.setGender("男");
user.setAddress("gd");
user.setQq("1402773304");
user.setEmail("1402773304@qq.com");
user.setUsername("nice0e3");
user.setPassword("nize");
userDao.SaveUser(user);
}
public void Update(){
}
@After //测试方法执行后执行
public void destroy() throws IOException {
session.close();
in.close();
}
}
Put the configuration information in the init method, and it will run automatically and assign values at run time.
The resource to be closed is placed in the destroy method, and the resource is automatically closed when it is closed.
After encapsulation, our savetest method not only sets the value, but also completes the modification function in one line of code.
After debugging, the front and back are easy to write, which is basically copy and paste.
0x02 mybatis modification Dafa
After the previous framework is roughly defined, write a modification operation.
Add the updata method to the userdao interface:
void UpdataUser(User user);
Add updata label to the mapping file:
<update id="UpdataUser" parameterType="com.test.domain.User">
update user set username = #{username},address=#{address},age=#{age} where id=#{id}
</update>
Add a method based on the above code:
@Test
public void Update(){
user.setUsername("nnicee0e3");
user.setAddress("dgd");
user.setAge(18);
user.setId(8);
userDao.UpdataUser(user);
}
After running, the content was successfully modified.
0x03 mybatis delete Dafa
The operation of deleting is simpler.
Add a method to the userdao interface:
void deleteUser(Integer id);
Add the delete tag to the mapping file:
<delete id="deleteUser" parameterType="Integer">
delete from user where id = #{id}
</delete>
Copy and paste the following code in the test class:
@Test
public void delete() {
userDao.deleteUser(6);
}
0x04 mybatis query single message
As always, the interface addition method:
List<User> findone(Integer id);
As always, add the contents of the mapping file:
<select id="findone" resultType="com.test.domain.User" parameterType="int">
select * from user where id =#{id}
</select>
As always, call the method in the test class:
@Test
public void findone(){
List<User> findone = userDao.findone(1);
System.out.println(findone.toString());
}
0x05 mybatis fuzzy query method
Interface addition method:
List<User> findlike(String name);
Add mapping file content:
<select id="findlike" resultType="com.test.domain.User" parameterType="string">
select * from user where name like #{name}
</select>
Call method in test class:
@Test
public void findlike(){
List<User> ming = userDao.findlike("%xiao%");
for (User user1 : ming) {
System.out.println(user1);
}
}
0x06 end
As long as you can use the basic addition, deletion, modification, query and definition of SQL statements, you can copy and paste others.