Addition, deletion and modification of mybatis - idea

01? 2022-02-13 07:30:08 阅读数:140

addition deletion modification mybatis idea

1、 establish maven engineering , And create the corresponding package , class , Interface , The configuration file

 Wechat screenshot _20211218212833.png

2、pom.xml Import jar Packet dependency

 <dependencies>
<!-- mysql drive -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--mybatis what is needed jar package -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<!-- Solve the problem of resource export -->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
Copy code 

3、 To configure mybatis-config The configuration file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- Import external profile -->
<properties resource="db.properties"/>
<typeAliases>
<!-- 1、 You can alias entity classes , Entity classes are rarely used -->
<!-- <typeAlias type="pojo.User" alias="User"/>-->
<!-- 2、 You can alias entity classes , The default alias is lowercase
If you have any notes , Take annotation as alias -->
<package name="pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- Connect to database , The database USES mysql8.0-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- Bind configuration resources , register mapper, Each one needs to be registered -->
<mappers>
<!-- 1、 resources Binding requires the use of Lujin , Recommended -->
<mapper resource="dao/UserMapper.xml"/>
<!-- 2、 Use class Binding registration
Interface and mapper The configuration file must have the same name
Interface and mapper Must be in the same bag
<mapper class="dao.UserMapper"/>
-->
<!-- 3、 Use package scanning to register
Interface and mapper The configuration file must have the same name
Interface and mapper Must be in the same bag
<package name="dao"/>
-->
</mappers>
</configuration>
Copy code 

4、 To configure db.properties file

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ database ?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
username= Database user name
password= Database password
Copy code 

5、 Write a tool class MyBatisUntil

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 java.io.IOException;
import java.io.InputStream;
// Tool class
public class MyBatisUntil {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
// obtain sqlSessionFactory object
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
// establish sqlSession object
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
Copy code 

6、 Create database tables , And the corresponding entity class

create table user(

id integer auto_increment PREPARE key,

name varchar(20),

pwd varchar(20)

);

public class User {
private int id;
private String name;
private String pwd;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + ''' +
", pwd='" + pwd + ''' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
Copy code 

7、 Create an interface

package dao;
import pojo.User;
import java.util.List;
import java.util.Map;
public interface UserMapper {
// Get all users
List<User> getUserList();
// according to id Query the user
User getUserById(int id);
// Add users
public int AddUser(User user);
// Modify the user
int updateUser(User user);
// Delete user
int deleteUser(int id);
}
Copy code 

8、 Create corresponding interface UserMapper.xml The configuration file

<?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">
<!-- Binding correspondence mapper Interface , Bind here UserDao Interface -->
<mapper namespace="dao.UserMapper">
<!-- Query statement ,id Corresponding UserDao Method name ,resultType Is the full class name -->
<!-- User After the alias , Use alias directly -->
<select id="getUserList" resultType="user">
select * from user
</select>
<!--parameterType Parameter type -->
<select id="getUserById" parameterType="int" resultType="pojo.User">
select * from user where id = #{id}
</select>
<insert id="AddUser" parameterType="pojo.User" >
insert into user (id,name ,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="pojo.User">
update user set name=#{name },pwd=#{pwd} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
Copy code 

9、 To mybatis-config Register in profile UserMapper

 <mappers>
<!-- 1、 resources Binding requires the use of Lujin , Recommended -->
<mapper resource="dao/UserMapper.xml"/>
<!-- 2、 Use class Binding registration
Interface and mapper The configuration file must have the same name
Interface and mapper Must be in the same bag
<mapper class="dao.UserMapper"/>
-->
<!-- 3、 Use package scanning to register
Interface and mapper The configuration file must have the same name
Interface and mapper Must be in the same bag
<package name="dao"/>
-->
</mappers>
Copy code 

10、 Create test class

package dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import until.MyBatisUntil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserDaoTest {
@Test
public void test1(){
// obtain sqlSession object
SqlSession sqlSession = MyBatisUntil.getSqlSession();
// perform
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// Loop print find data
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
// close
sqlSession.close();
}
@Test
public void test2(){
SqlSession sqlSession = MyBatisUntil.getSqlSession();
// Get class mapper Interface
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.getUserById(1));
sqlSession.close();
}
// To add, delete, or modify, a transaction must be submitted
@Test
public void test3(){
SqlSession sqlSession = MyBatisUntil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.AddUser(new User(4, " Little ", "12345"));
// Commit transaction , Don't miss
sqlSession.commit();
sqlSession.close();
}
@Test
public void test4(){
SqlSession sqlSession = MyBatisUntil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4," nap ","2323"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void test5(){
SqlSession sqlSession = MyBatisUntil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}
Copy code 

to be finished

 

copyright:author[01?],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130730061797.html