mybatis系列教程(一)——mybatis配置
- 2019 年 10 月 30 日
- 筆記
版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/luo4105/article/details/72736534
項目地址
項目地址:https://code.csdn.net/luo4105/study_mybatis/
Mybatis單獨配置
為了更好了解mybatis的配置,先完成一個不帶spring整合的mybatis配置例子。
使用mybatis的配置如下
1.配置數據源,數據庫、賬號密碼等
2.配置mapper配置文件
3.通過sqlsession獲得對應dao接口,操作dao接口。代碼
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("config/mybatis-config.xml")); sqlSession = sqlSessionFactory.openSession(); usersDao = sqlSession.getMapper(UsersDao.class);
實現
Maven依賴
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
項目結構

Sql建表語句
CREATE TABLE`users`( `id` int(11) NOTNULLAUTO_INCREMENT, `name`varchar(200) DEFAULT NULL, `age` int(11) DEFAULTNULL, PRIMARYKEY(`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO study_mybatis.users(id,name,age) VALUES (1,'小王',10) ,(2,'荀麗',29) ,(3,'明明',12) ,(4,'天天',13);
配置Mybatis
Mybatis配置文件mybatis-config.xml,這裡配置的數據源信息和mapper。我用的是mysql-connection-java6.x,6.x版本的數據庫連接驅動改為com.mysql.cj.jdbc.Driver,之前是com.mysql.jdbc.Driver。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPEconfigurationPUBLIC"-//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.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/study_mybatis?serverTimezone=GMT&characterEncoding=utf-8"/> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/lc/mapper/UsersMapper.xml" /> </mappers> </configuration>
Mapper、對應接口和model
UserMapper.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lc.dao.UsersDao"> <select id="getList" resultType="com.lc.model.Users"> select id, name, age from users </select> <insert id="add" parameterType="com.lc.model.Users"> INSERT INTO users(name, age)VALUES (#{name}, #{age}) </insert> </mapper>
接口
importcom.lc.model.Users; import java.util.List; public interface UsersDao { public List<Users> getList(); public int add(Users users); }
實體類:User
public classUser { private Integer id; private String name; private Integer age; /** Getter、setter、toString **/ }
測試類
UserDaoTest
importcom.lc.model.Users; 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.Before; import org.junit.Test; import java.io.IOException; import java.util.List; public class UsersDaoTest { private UsersDao usersDao; private SqlSession sqlSession; @Before public void before() throws IOException { SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("config/mybatis-config.xml")); sqlSession = sqlSessionFactory.openSession(); usersDao = sqlSession.getMapper(UsersDao.class); } @Test public void getList() throws Exception { List<Users> usersList =usersDao.getList(); for (Users users : usersList) { System.out.println(users.toString()); } } @Test public void add() throws Exception { Users user = new Users(); user.setAge(18); user.setName("同光塵"); usersDao.add(user); sqlSession.commit(); } }
總結
在mybatis的配置文件中,主要配置了兩個
dataSource:數據源
mappers:mapper映射文件
在測試類中,實現流程如下
獲得sqlsessionFactory
獲得SqlSessionFactory對象
獲得SqlSession對象
獲得UserMapper對象
操作UserMapper對象
進一步猜想
如果我們把數據源和mapper的映射配置在spring的配置文件,
把SqlSessionFactory對象、SqlSession對象、UserMapper對象都在Spring中註冊,
是不是就完成了spring整合mybatis,就不需要mybatis的配置文件了。