Mybatis的使用CRUD
数据库
CREATE DATABASE mybatis; USE mybatis; CREATE TABLE USER( id INT PRIMARY KEY AUTO_INCREMENT NOT NULL, NAME VARCHAR(30) DEFAULT NULL, pwd VARCHAR(30) DEFAULT NULL )ENGINE=INNODB DEFAULT CHARSET=utf-8; INSERT INTO USER(id,NAME,pwd) VALUES (1,"Levi.Zhao","123456"), (2,"test","test"), (3,"admin","admin"), (4,"root","root");
UserDao.java
package top.withlevi.dao; import top.withlevi.domain.User; import java.util.List; public interface UserDao { // 查询所有数据 List<User> getUserList(); // 查询某个数据 User getUserById(int id); // 添加数据 int addUser(User user); // 修改数据 int updateUser(User user); // 删除数据 int delUser(int id); }
UserMapperDao.xml
<?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="top.withlevi.dao.UserDao"> <!--查询全部数据--> <select id="getUserList" resultType="top.withlevi.domain.User"> select * from mybatis.user; </select> <!--查询某个数据--> <select id="getUserById" parameterType="top.withlevi.domain.User" resultType="top.withlevi.domain.User"> select * from mybatis.user where id = #{id}; </select> <!--添加数据--> <insert id="addUser" parameterType="top.withlevi.domain.User"> insert into mybatis.user(id, name, pwd) values (#{id},#{name},#{pwd}); </insert> <!--修改数据--> <update id="updateUser" parameterType="top.withlevi.domain.User" > update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}; </update> <!--删除数据--> <delete id="delUser" parameterType="int"> delete from mybatis.user where id=#{id}; </delete> </mapper>
User.java
package top.withlevi.domain; public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.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; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
MybatisUtils.java
package top.withlevi.utils; 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; //sqlSessionFactory --> sqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ String resource = "mybatis-config.xml"; try { InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
resources->mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--configuration核心配置文件--> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="top/withlevi/dao/UserMapperDao.xml"></mapper> </mappers> </configuration>
parent->pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--父工程--> <groupId>top.withlevi</groupId> <artifactId>Mybatis-Study</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>Mybatis-01</module> </modules> <!--导入依赖--> <dependencies> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project>
kid->pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>Mybatis-Study</artifactId> <groupId>top.withlevi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Mybatis-01</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
TestDao.java
package top.withlevi.test; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import top.withlevi.dao.UserDao; import top.withlevi.domain.User; import top.withlevi.utils.MybatisUtils; import java.util.List; public class TestDao { @Test public void Test01(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); List<User> userList = mapper.getUserList(); for (User user : userList) { System.out.println(user); } sqlSession.close(); } @Test public void Test02(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); User user = mapper.getUserById(3); System.out.println(user); sqlSession.close(); } @Test public void Test03(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); int result = mapper.addUser(new User(5, "赵增印", "iBj@2021")); if (result>0){ System.out.println("add successful...."); } // 提交事务 sqlSession.commit(); sqlSession.close(); } @Test public void Test04(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); int i = mapper.updateUser(new User(2, "cnbj", "hahhaha")); if (i>0){ System.out.println("update successful..."); } sqlSession.commit(); sqlSession.close(); } @Test public void Test05(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); int i = mapper.delUser(2); if (i>0){ System.out.println("delete data successful..."); } sqlSession.commit(); sqlSession.close(); } }