在MyBatis的第一篇的学习中我们了解了MyBatis是什么和为什么学习MyBatis,本次我们将学习MyBatis的具体使用。
思路:环境搭建——>MyBatis导入——>代码编写——>测试。
一、环境搭建
1.建立数据库- CREATE DATABASE `MyBaties`;
- CREATE TABLE `user`(
- `id` INT(20) NOT NULL PRIMARY KEY,
- `username` VARCHAR(30) NOT NULL,
- `password` VARCHAR(30) NOT NULL
- );
- INSERT INTO `user` (`id`,`username`,`password`)
- VALUES
- (10000,'jms1','123456'),
- (10001,'jms2','123456'),
- (10003,'jms3','123456')
复制代码 2.建立一个maven项目
这个项目作为父项目。
修改pom.xml导入需要的依赖:mysql驱动、mydatis、junit。- <dependencies>
-
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><dependency>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <groupId>mysql</groupId>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <artifactId>mysql-connector-java</artifactId>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <version>8.0.30</version>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers></dependency>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><dependency>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <groupId>org.mybatis</groupId>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <artifactId>mybatis</artifactId>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <version>3.5.10</version>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers></dependency>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><dependency>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <groupId>junit</groupId>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <artifactId>junit</artifactId>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <version>4.13.2</version>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <scope>test</scope>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers></dependency>
- </dependencies>
复制代码 3.建立一个子模块
(1)编写mybatis的核心配置文件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">
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><property name="driver" value="${driver}"/>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><property name="url" value="${url}"/>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><property name="username" value="${username}"/>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><property name="password" value="${password}"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="org/mybatis/example/BlogMapper.xml"/>
- </mappers>
- </configuration>
复制代码 这是我个人的使用:- <?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">
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><property name="driver" value="com.mysql.jdbc.Dirver"/>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><property name="url" value="jdbc:mysql://localhost:3306/mybaties?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><property name="username" value="root"/>
- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers><property name="password" value="123456"/>
- </dataSource>
- </environment>
- </environments>
- </configuration>
复制代码 (2)编写mybatis工具类MyBatisUtil.class
每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先配置的 Configuration 实例来构建出 SqlSessionFactory 实例。
从 XML 文件中构建 SqlSessionFactory 的实例非常简单,建议使用类路径下的资源文件进行配置。 但也可以使用任意的输入流(InputStream)实例,比如用文件路径字符串或 file:// URL 构造的输入流。MyBatis 包含一个名叫 Resources 的工具类,它包含一些实用方法,使得从类路径或其它位置加载资源文件更加容易。
官方文档给出了以下三行代码:- String resource = "org/mybatis/example/mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
复制代码 我们要做的就是把这三行代码封装到MyBatisUtil.class中:- 1 package com.jms.utils;
- 2
- 3
- 4 import java.io.IOException;
- 5 import java.io.InputStream;
- 6
- 7 import org.apache.ibatis.io.Resources;
- 8 import org.apache.ibatis.session.SqlSession;
- 9 import org.apache.ibatis.session.SqlSessionFactory;
- 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- 11
- 12 //SqlSessionFactory-->SqlSession
- 13 public class MyBatisUtil {
- 14
- 15 private static SqlSessionFactory sqlSessionFactory;
- 16
- 17 //获取SqlSessionFactory对象
- 18 static {
- 19 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> try {
- 20 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> String resource = "mybatis-config.xml";
- 21 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> InputStream inputStream = Resources.getResourceAsStream(resource);
- 22 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- 23 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> } catch (IOException e) {
- 24 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> e.printStackTrace();
- 25 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> }
- 26 }
- 27
- 28 //通过SqlSessionFactory获取SqlSession对象,其中包含了面向数据库执行执行SQL命令所需要的方法
- 29 public static SqlSession getSqlSession() {
- 30 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> return sqlSessionFactory.openSession();
- 31 }
- 32 }
复制代码 官网上也有着不进行核心配置,直接用纯代码进行编写的模式,本文就不做过多赘述,可自行了解。
3.编写代码
(1)首先我们要写一个实体类user.class,这个实体类应该与我们前面数据库建立的user表对应。- 1 package com.jms.pojo;
- 2
- 3 public class user {
- 4
- 5 private int id;
- 6 private String username;
- 7 private String password;
- 8
- 9 public user() {
- 10 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>
- 11 }
- 12
- 13 public user(int id, String username, String password) {
- 14 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> super();
- 15 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> this.id = id;
- 16 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> this.username = username;
- 17 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> this.password = password;
- 18 }
- 19
- 20 public int getId() {
- 21 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> return id;
- 22 }
- 23 public void setId(int id) {
- 24 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> this.id = id;
- 25 }
- 26 public String getUsername() {
- 27 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> return username;
- 28 }
- 29 public void setUsername(String username) {
- 30 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> this.username = username;
- 31 }
- 32 public String getPassword() {
- 33 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> return password;
- 34 }
- 35 public void setPassword(String password) {
- 36 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> this.password = password;
- 37 }
- 38
- 39 @Override
- 40 public String toString() {
- 41 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> return "user [id=" + id + ", username=" + username + ", password=" + password + "]";
- 42 }
- 43 }
复制代码 (2)编写一个Mapper接口UserMapper- 1 package com.jms.dao;
- 2
- 3 import java.util.List;
- 4
- 5 import com.jms.pojo.User;
- 6
- 7 public interface UserMapper {
- 8
- 9 List<User> getUsers();
- 10
- 11 }
复制代码 (3)编写Mapper配置文件UserMapper.xml
一般来说接口都会有实现类,但是这里我们用Mapper配置文件来代替了实现类。- <?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.jms.dao.userMapper">
-
- <select id="getUsers" resultType="com.jms.pojo.User">
- select * from mybaties.user
- </select>
- </mapper>
复制代码 4.注册Mapper
修改mybatis的核心配置文件mybatis-config.xml,添加以下三行:- <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>
复制代码 resource对应的是实现接口的Mapper的路径。
5.junit测试
建立测试类UserMapperTest.class- package com.jms.dao;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import com.jms.pojo.User;import com.jms.utils.MyBatisUtil;public class UserMapperTest { <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>@Test public void test() { <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>//利用工具类获取SqlSession <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>SqlSession sqlSession = MyBatisUtil.getSqlSession(); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>try { <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> //方法一 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> //利用SqlSession获取UserMapper接口 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> UserMapper userMapper = sqlSession.getMapper(UserMapper.class); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> //调用方法 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> List users = userMapper.getUsers(); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>//方法二,老方法不推荐用 <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> List users2 = sqlSession.selectList("com.jms.dao.UserMapper.getUsers"); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>System.out.println("这是方法一的结果"); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> for(User user: users) { <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>System.out.println(user); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> } <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> System.out.println("这是方法二的结果"); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> for(User user: users2) { <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>System.out.println(user); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> } <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> } catch(Exception e) { <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> e.printStackTrace(); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>} finally{ <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers> sqlSession.close(); <mappers>
- <mapper resource="com/jms/dao/UserMapper.xml"/>
- </mappers>} } }
复制代码 上面有两种方法,方法二不推荐。
SqlSession用完后应及时关闭。
测试结果如下:
可以看到成功地查到了数据库中的信息。
(本文仅作个人学习记录用,如有纰漏敬请指正)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |