mybatis實現(xiàn)CRUD詳解(使用mapper映射文件實現(xiàn)增刪改查)
創(chuàng)建maven項目:項目結(jié)構(gòu)如圖所示
準備數(shù)據(jù)庫表:
準備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>com.etime</groupId>
<artifactId>day09</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 添加lombok依賴工具使用其中的注解方法可以讓實體類中g(shù)et,set,以及實體類中操作少一些-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<!-- 添加和引入mybatis的版本號等依賴-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 這里添加mysql依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
<!-- 如果不需要也可不進行單元測試的依賴引入-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
編寫核心配置文件加載所需要的資源
編寫config.xml文件
<?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>
<!-- 配置 mybatis的環(huán)境-->
<environments default="development">
<!-- 配置環(huán)境-->
<environment id="development">
<!-- 配置事物類型-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置連接數(shù)據(jù)庫的信息:用的是數(shù)據(jù)源[連接池]-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!-- jdbc:mysql://localhost:3306/db_school?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC-->
<!-- 和javaWeb servlet三層架構(gòu)中的區(qū)別這里是只需要設(shè)置時區(qū)就可以了-->
<property name="url" value="jdbc:mysql://localhost:3306/db_school?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="h123456"/>
</dataSource>
</environment>
</environments>
<!-- 注冊StudentDao接口映射文件位置-->
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
創(chuàng)建工廠連接數(shù)據(jù)處理工具SqlSessionUtil.java
SqlSessionUtil.java
package com.etime.util;
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;
public class SqlSessionUtil {
private static SqlSession sqlSession =null;
static {
//加載配置文件
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream("config.xml");
} catch (IOException e) {
e.printStackTrace();
}
//用于讀取配置文件內(nèi)容,生成SqlSessionFactory
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
//獲取SqlSession對象
sqlSession = sqlSessionFactory.openSession();
}
public SqlSession getSqlSession(){
return sqlSession;
}
}
創(chuàng)建學生實體類對象Student.java
package com.etime.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//添加無參構(gòu)造函數(shù)
@NoArgsConstructor
//添加全參數(shù)構(gòu)造函數(shù)
@AllArgsConstructor
//添加所有需要的get,set等方法
@Data
public class Student {
private int sid;
private String sname;
private String sgender;
private int sage;
private String semail;
private String sphoto;
}
實現(xiàn)增、刪、改、查
1、查詢所有學生信息:
創(chuàng)建接口StudentDao.java:向其中添加查詢所有學生信息
package com.etime.dao;
import com.etime.pojo.QueryVo;
import com.etime.pojo.Student;
import java.util.List;
import java.util.Map;
public interface StudentDao {
//查詢所有學生信息
List<Student> getAllStudents();
}
創(chuàng)建StudentMapper.xml文件編寫sql映射
StudentMapper.xml
<?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">
<!--namespace是當前mapper對應的dao接口-->
<mapper namespace="com.etime.dao.StudentDao">
<!-- select指定當前進行數(shù)據(jù)庫操作的是查詢-->
<!-- id的值對應的是當前dao層接口中的方法名字-->
<!-- resultType指定當前查詢得到的數(shù)據(jù)要封裝成的類型-->
<select id="getAllStudents" resultType="com.etime.pojo.Student">
select * from student
</select>
</mapper>
編寫MybatisTest.java文件編寫測試方法:因在上述中已經(jīng)將SqlSession的操作提成一個工具類所以測試類中就不需要再些SqlSession.java的部分操作,只需要在測試類中創(chuàng)建 該類對象,進行引用
package com.etime.test;
import com.etime.dao.StudentDao;
import com.etime.pojo.QueryVo;
import com.etime.pojo.Student;
import com.etime.pojo.Teacher;
import com.etime.util.SqlSessionUtil;
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 org.junit.experimental.theories.suppliers.TestedOn;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
public class MybatisTest {
SqlSessionUtil sqlSessionUtil = new SqlSessionUtil();
//查詢所有學生信息
@Test
public void t01(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
//獲取StudentDao對象
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
//調(diào)用方法獲取結(jié)果
List<Student> list = studentDao.getAllStudents();
for (Student student:list
) {
System.out.println(student);
}
sqlSession.close();
}
}
運行結(jié)果:
2、實現(xiàn)對學生進行信息添加
向StudentDao.java接口中添加學生方法:
//添加學生
int addStudent(Student student);
向StudentMapper.xml映射文件中添加添加學生的sql映射
<insert id="addStudent" parameterType="com.etime.pojo.Student">
insert into student(sname,sage,sgender,semail,sphoto) values (#{sname},#{sage},#{sgender},#{semail},#{sphoto});
</insert>
向測試類中編寫添加學生信息方法:
//添加學生
@Test
public void t02() throws IOException {
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
int rows = studentDao.addStudent(new Student(0,"阿坤","男",18,"shhfifw@qq.com","djia.jpg"));
System.out.println(rows);
//SqlSession在做增刪改操作時,需要手動提交
sqlSession.commit();
sqlSession.close();
}
運行結(jié)果:
3、根據(jù)學生id刪除學生信息
向StudentDao.java接口中添加根據(jù)學生id刪除學生信息
//根據(jù)學生id刪除學生信息
int delStudent(int sid);
向StudentMapper.xml映射文件中添加根據(jù)學生id刪除學生信息sql映射
<delete id="delStudent" parameterType="int">
delete from student where sid=#{sid}
</delete>
向測試類中MybatisTest.java中編寫根據(jù)學生id刪除學生信息sql映射
//根據(jù)學生id刪除學生
@Test
public void t03(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
int rows = studentDao.delStudent(34);
System.out.println(rows);
//SqlSession在做增刪改操作時,需要手動提交
sqlSession.commit();
sqlSession.close();
}
運行結(jié)果:
4、更新學生信息
向StudentDao.java接口中編寫修改學生信息方法
//更新學生信息
int updateStudent(Student student);
向映射文件StudentMapper.xml文件中添加學生信息更新sql映射
<update id="updateStudent" parameterType="com.etime.pojo.Student">
update student set sname=#{sname},sage=#{sage},sgender=#{sgender},semail=#{sphoto}
</update>
向測試類中編寫測試學生信息更新的方法
//根據(jù)學生id更新學生信息
@Test
public void t04(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
int rows = studentDao.updateStudent(new Student(12,"坤坤","男",19,"djwoie@qq.com","dag.jpg"));
System.out.println(rows);
//SqlSession在做增刪改操作時,需要手動提交
sqlSession.commit();
sqlSession.close();
}
運行結(jié)果:
5、根據(jù)學生id獲取學生信息
向StudentDao.java中編寫根據(jù)學生id獲取學生信息的方法
//根據(jù)學生id獲取學生信息
Student getStuBySid(int sid);
向映射文件StudentMapper.xml中編寫根據(jù)學生id獲取學生信息
<select id="getStuBySid" parameterType="int" resultType="com.etime.pojo.Student">
select * from student where sid=#{sid}
</select>
向測試文件中編寫根據(jù)學生id獲取學生信息
//根據(jù)學生id獲取學生信息
@Test
public void t05(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
Student student = studentDao.getStuBySid(33);
System.out.println(student);
sqlSession.close();
}
運行結(jié)果:
6、獲取學生表中的所有數(shù)據(jù)條數(shù)
向StudentDao.java接口中編寫查詢所有數(shù)據(jù)條數(shù)方法
//獲取學生數(shù)據(jù)條數(shù)
int getCountRows();
向映射文件中編寫查詢獲取學生數(shù)據(jù)條數(shù)的sql映射
<select id="getCountRows" resultType="int">
select count(*) from student
</select>
向測試類MybatisTest.java中編寫查詢所有學生數(shù)據(jù)條數(shù)的測試方法
//獲取學生表的所有數(shù)據(jù)條數(shù)
@Test
public void t06(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
int rows=studentDao.getCountRows();
System.out.println(rows);
sqlSession.close();
}
運行結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-422069.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-422069.html
到了這里,關(guān)于mybatis實現(xiàn)CRUD詳解(使用mapper映射文件實現(xiàn)增刪改查)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!