流程:搭建环境–>导入Mybatis–>编写代码–>测试
一、搭建环境
1.1 搭建数据库
1 2 3 4 5 6 7 8 9 10 11 12
| CREATE DATABASE `mybatis`; USE `mybatis`; CREATE TABLE `user`( `id` INT(20) NOT NULL PRIMARY KEY, `name` VARCHAR(30) DEFAULT NULL, `pwd` VARCHAR(30) DEFAULT NULL )ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `user`(`id`,`name`,`pwd`) VALUES (1,'倪矗1','1234'), (2,'倪矗2','123456'), (3,'倪矗3','12346'), (4,'倪矗4','123654')
|
1.2 新建项目(作为父项目)
- 新建一个普通的Maven项目
- 删除src文件
- 导入jar包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> </dependency> </dependencies>
|
1.3 创建模块(作为子项目,子项目可以直接使用父项目的依赖)
二、配置第一个Mybatis程序
目录结构:
2.1 编写mybatis工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } }
public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
|
2.2 编写mybatis核心配置文件
在resources目录中新建mybatis-config.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?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.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="nc20011019"/> </dataSource> </environment> </environments>
</configuration>
|
2.3 编写数据库每张表的实体类pojo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.nichu.pojo;
public class User { private int id; private String name; private String pwd;
public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
|
2.4 编写Dao层接口
1 2 3 4 5 6 7
| package com.nichu.dao; import com.nichu.pojo.User; import java.util.List;
public interface UserDao { List<User> getUserList(); }
|
2.5 编写配置文件绑定接口并实现对数据库的操作
1 2 3 4 5 6 7 8 9 10 11 12
| <?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.nichu.dao.UserDao"> <select id="getUserList" resultType="com.nichu.pojo.User"> select * from mybatis.user </select> </mapper>
|
2.6 在核心配置文件中添加映射
1 2 3
| <mappers> <mapper resource="com/nichu/dao/UserMapper.xml"></mapper> </mappers>
|
2. 7 编写测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import com.nichu.dao.UserDao; import com.nichu.pojo.User; import com.nichu.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test;
import java.util.List;
public class UserDaoTest { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao UserDao = sqlSession.getMapper(UserDao.class); List<User> userList = UserDao.getUserList();
List<User> uu = sqlSession.selectList("com.nichu.dao.UserDao.getUserList"); for (User user : uu) { System.out.println(user); } sqlSession.close(); } }
|
三、关于在maven项目中配置文件资源导出问题
标准的Maven项目都会有一个resources目录来存放我们所有的资源配置文件,但是我们往往在项目中不仅仅会把所有的资源配置文件都放在resources中,同时我们也有可能放在项目中的其他位置,那么默认的maven项目构建编译时就不会把我们其他目录下的资源配置文件导出到target目录中,就会导致我们的资源配置文件读取失败,从而导致我们的项目报错出现异常,比如说尤其我们在使用MyBatis框架时,往往Mapper.xml配置文件都会放在dao包中和dao接口类放在一起的,那么执行程序的时候,其中的xml配置文件就一定会读取失败,不会生成到maven的target目录中,所以我们要在项目的pom.xml文件中进行设置,并且我建议大家,每新建一个maven项目,就把该设置导入pom.xml文件中,以防不测!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
|