1.開發環境 新建一個工程-配置jdk版本-配置maven 2.創建maven工程 修改打包方式為jar 導入依賴: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
1.開發環境
新建一個工程-配置jdk版本-配置maven2.創建maven工程
修改打包方式為jar導入依賴:
<?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.tinstu.mybatis</groupId>
<artifactId>MyBatis_demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- Mybatis核心 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- junit測試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.3</version>
</dependency>
</dependencies>
</project>
3.創建MyBatis的核心配置文件 mybatis-config.xml
在resources下創建mybatis-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>
<!--設置連接資料庫的環境-->
<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"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<mapper resource="mappers/UserMapper.xml"/>
</mappers>
</configuration>
4.創建mapper介面 UserMapper.java
MyBatis中的mapper介面相當於以前的dao,但是區別於,mapper僅僅是介面,我們不需要提供實現類先在 mybatis-config.xml中配置好資料庫信息,創建一個資料庫,創建實體類User.java
創建一個介面 UserMapper.java
package com.tinstu.mybatis.mapper;
public interface UserMapper {
//添加用戶信息
int insertUser();
}
5.創建MyBatis的映射文件 UserMapper.xml
MyBatis面向介面編程的兩個一致- 1.映射文件UserMapper.xml的 namespace和mapper介面UserMapper.java的全類名保持一致
<mapper namespace="com.tinstu.mybatis.mapper.UserMapper">
- 2.映射文件中sql語句的id要和mapper介面中的方法名一致 (此映射文件中事務id=insertUser和上面介面中的方法名insertUser一樣)
<?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.tinstu.mybatis.mapper.UserMapper">
<!--int insertUser();-->
<insert id="insertUser">
insert into t_user values(null,'張三','123','[email protected]')
</insert>
</mapper>
最後還要在配置文件mybatis-config.xml中引入映射文件
<!--引入映射文件-->
<mappers>
<mapper resource="mappers/UserMapper.xml"/>
</mappers>
6.測試添加功能
創建一個測試類package com.tinstu.mybatis;
import com.tinstu.mybatis.mapper.UserMapper;
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;public class Mybatistest {
@Test
public void testmybatis() throws IOException{
//載入核心配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//獲取SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//獲取sqlSessionFactory
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//獲取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//獲取mapper介面對象
UserMapper mapper = sqlSession.getMapper(UserMapper.class) ;
//測試功能
int result = mapper.insertUser();
//提交事務
sqlSession.commit();}
}
7.優化功能
1.SqlSession預設不自動提交事務,如需要自動提交事務,可以使用SqlSessionFactory.openSession(true);
//獲取SqlSession(加入true)
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//獲取mapper介面對象
UserMapper mapper = sqlSession.getMapper(UserMapper.class) ;
//測試功能
int result = mapper.insertUser();
//提交事務(刪掉)
// sqlSession.commit();
2.加入log4j日誌功能
(1)加入依賴<!-- log4j日誌 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
-
log4j的配置文件名為log4j.xml,存放的位置是src/main/resources目錄下
-
-
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <param name="Encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" /> </layout> </appender> <logger name="java.sql"> <level value="debug" /> </logger> <logger name="org.apache.ibatis"> <level value="info" /> </logger> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration>
8.測試刪除修改功能
(1)修改UserMapper.xml中加入
<!--void updateUser();-->
<update id="updateUser">
update user set username = '張三55' where id = 3 ;
</update>
測試類
@Test
public void testuodate() throws IOException{
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser();
}
刪除:
<!--void deleteUser();-->
<delete id="deleteUser">
delete from user where id = 5
</delete>
9.測試查詢功能
(1)根據id查詢UserMapper.java中添加查詢方法
//查詢功能
User getUser();
UserMapper.xml中寫sql語句
<!--void getUser();-->
<!--
查詢功能的標簽必須設置resultType或resultMap
resultType:設置預設的映射關係
resultMap:設置自定義的映射關係
-->
<select id="getUser" resultType="com.tinstu.mybatis.pojo.User">
select * from user where id=3
</select>
測試類:
@Test
public void testuodate() throws IOException{
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUser();
System.out.println(user);
}
(2)查詢所有
UserMapper.java中添加查詢方法
//查詢所有
List<User> getUserAll();
UserMapper.xml中寫sql語句
<!--List<User> getUserAll();-->
<select id="getUserAll" resultType="com.tinstu.mybatis.pojo.User">
select * from user
</select>
測試類:
@Test
public void testuodate() throws IOException{
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserAll();
System.out.println(userList);
//lanmbda表達式
userList.forEach(user -> System.out.println(user));
}
我的java筆記:mybatis環境搭建及測試: https://www.tinstu.com/1218.html
本文來自博客園,作者:Tinstu.com,轉載請註明原文鏈接:https://www.cnblogs.com/tinstu/p/16191089.html