如何通過XML方式配置并實現(xiàn)Mybatis
idea中創(chuàng)建一個maven項目
在pom文件中導(dǎo)入下面的依賴
<!--mybatis核心包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!--mysql數(shù)據(jù)庫驅(qū)動包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <!--log4j日志包--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
創(chuàng)建一個java源文件夾和resources資源文件夾并準(zhǔn)備好mybatis配置文件mybaits.xml和數(shù)據(jù)庫文件db.properties

mybaits.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>
<!--數(shù)據(jù)庫參數(shù)文件-->
<properties resource="db.properties"></properties>
<!--別名的配置,可以不用-->
<typeAliases>
<package name="cn.cqy.domain"></package>
</typeAliases>
<!--配置環(huán)境,可以多個,這里要設(shè)置一個默認(rèn)使用的環(huán)境-->
<environments default="dev">
<!--配置環(huán)境名,唯一一個id名稱-->
<environment id="dev">
<!--事務(wù)管理 type:JDBC(支持事務(wù))/MANAGED(什么都不做)-->
<transactionManager type="JDBC"></transactionManager>
<!--數(shù)據(jù)源, 連接池 type(POOLED):MyBatis自帶的連接池-->
<dataSource type="POOLED">
<property name="driver" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</dataSource>
</environment>
</environments>
<!--這個mappers代表的是相應(yīng)的ORM映射文件 這里先準(zhǔn)備好了-->
<mappers>
<mapper resource="cn/cqy/mapper/StudentMapper.xml"></mapper>
</mappers>
</configuration>
db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/prc?userUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username=root
password=root
數(shù)據(jù)庫準(zhǔn)備
準(zhǔn)備相應(yīng)的對象
創(chuàng)建一個Student對象,和數(shù)據(jù)庫的表對應(yīng)
public class Student {
private String s_id;
private String s_name;
private String s_birth;
private String s_sex;
public Student() {}
//getter,setter 方法省略
}
mapper的準(zhǔn)備 ,創(chuàng)建一個mapper文件夾,并在內(nèi)創(chuàng)建一個StudentMapper接口
public interface StudentMapper {
//查找學(xué)生表全部信息
public List<Student> selectAll();
//根據(jù)姓名查找學(xué)生
public Student selectByName(String name);
//插入一條學(xué)生信息
public void insertOne(Student stu);
//根據(jù)姓名刪除一條學(xué)生信息
public void deleteByName(String name);
//根據(jù)姓名修改一條學(xué)生信息
public void updateByName(Student stu);
}
在resoures資源文件夾下創(chuàng)建和mapper文件夾路徑相同的文件夾

然后創(chuàng)建映射文件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">
<!--命名空間 必須和所對應(yīng)接口的全限定名一致-->
<mapper namespace="cn.cqy.mapper.StudentMapper">
<!--定義sql的標(biāo)簽的id,需要和對應(yīng)接口的方法名一致 resultType的類型在沒有配置別名的情況下,應(yīng)該是POJO類的全限定名 如cn.cqy.domain.Student-->
<select id="selectAll" resultType="Student">
SELECT s_id,s_name,s_birth,s_sex FROM student
</select>
<!--參數(shù)類型為自定類型沒有別名,輸入類型全限定名,為Java類型時輸入其對應(yīng)映射名 如 long:大Long _long:小long (具體的對應(yīng)請參見文檔)-->
<select id="selectByName" resultType="Student" parameterType="String">
SELECT s_id,s_name,s_birth,s_sex FROM student
WHERE s_name = #{name}
</select>
<insert id="insertOne" parameterType="Student">
INSERT INTO student (s_id,s_name,s_birth,s_sex) VALUES (#{s_id},#{s_name},#{s_birth},#{s_sex})
</insert>
<delete id="deleteByName" parameterType="String">
DELETE FROM student where s_name = #{s_name}
</delete>
<update id="updateByName" parameterType="Student" >
UPDATE student SET s_birth = #{s_birth},s_sex = #{s_sex} WHERE s_name = #{s_name}
</update>
</mapper>
抽取出一個MybatisUtil工具類
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
//靜態(tài)代碼塊:在使用的時候先執(zhí)行,并且執(zhí)行一次
static {
try {
InputStream is = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//創(chuàng)建一個工廠實例(加載了我們的配置文件)
sqlSessionFactory = builder.build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*拿到了一個SqlSession對象
*/
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
public static void closeSqlSession(SqlSession sqlSession) {
if (sqlSession != null) {
sqlSession.close();
}
}
}
編寫測試類
public class SqlTest {
@Test
public void testSelectAll() {
//通過工具類獲得SqlSession對象
SqlSession sqlSession = MybatisUtil.getSqlSession();
//獲取到綁定到SqlSession的POJO類對應(yīng)的映射
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//通過映射調(diào)用查詢方法
List<Student> students = studentMapper.selectAll();
//關(guān)閉SqlSession
sqlSession.close();
for (Student student : students) {
System.out.println(student);
}
}
@Test
public void testSelectByName() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student stu = studentMapper.selectByName("吳蘭");
sqlSession.close();
System.out.println(stu);
}
@Test
public void testInsertOne() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setS_id("09");
stu.setS_name("李水");
stu.setS_birth("1988-08-22");
stu.setS_sex("男");
studentMapper.insertOne(stu);
//增、刪、改需要提交事務(wù),否則不會修改
sqlSession.commit();
sqlSession.close();
}
@Test
public void testDeleteByName() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
studentMapper.deleteByName("李水");
sqlSession.commit();
sqlSession.close();
}
@Test
public void testUpdateByName() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setS_name("李水");
stu.setS_birth("1999-01-22");
stu.setS_sex("女");
studentMapper.updateByName(stu);
sqlSession.commit();
sqlSession.close();
}
}
測試查詢結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java內(nèi)存溢出的幾個區(qū)域總結(jié)(注意避坑!)
內(nèi)存溢出是指應(yīng)用系統(tǒng)中存在無法回收的內(nèi)存或使用的內(nèi)存過多,最終使得程序運(yùn)行要用到的內(nèi)存大于虛擬機(jī)能提供的最大內(nèi)存,下面這篇文章主要給大家介紹了關(guān)于Java內(nèi)存溢出的幾個區(qū)域,總結(jié)出來給大家提醒注意避坑,需要的朋友可以參考下2022-11-11
IDEA2023版本創(chuàng)建Spring項目只能勾選17和21卻無法使用Java8的完美解決方案
想創(chuàng)建一個springboot的項目,本地安裝的是1.8,但是在使用Spring Initializr創(chuàng)建項目時,發(fā)現(xiàn)版本只有17和21,這篇文章主要介紹了IDEA2023版本創(chuàng)建Sping項目只能勾選17和21,卻無法使用Java8的解決方法,需要的朋友可以參考下2023-12-12
猜你不知道Spring Boot的幾種部署方式(小結(jié))
這篇文章主要介紹了猜你不知道Spring Boot的幾種部署方式(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

