1.创建maven的父子项目,方便我们管理项目
2.引入 我们要用到的jar包
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <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.2</version> </dependency> </dependencies>
3.配置 Mybatis核心配置文件 此文件一般放在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?characterEncoding=utf-8& serverTimezone=UTC&useSSL=false"/> <property name="username" value="root"/> <property name="password" value="12345"/> </dataSource> </environment> </environments> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
4.配置 Utils 工具类
public class MybatisUtils { //提升作用域 private static SqlSessionFactory sqlSessionFactory; static { try { //获取文件流 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); //利用 SqlSessionFactoryBuilder().build 创建sqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //获取SqlSession连接 public static SqlSession getSession(){ return sqlSessionFactory.openSession(); } }
5.编写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 + '\'' + '}'; } }
6.编写要用的接口
public interface UserDao { List<User> getUserList(); }
7.配置Mapper.xml 文件 这个xml我就按就相当于 原来在javaweb里用于实现dao里接口的实现类
<?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.chang.dao.UserDao"> <select id="getUserList" resultType="com.chang.pojo.User"> select * from user </select> </mapper>
8.编写测试类,测试代码
public class UserDaoTest { @Test public void test(){ SqlSession sqlsession = MybatisUtils.getSession(); //方式1 /* UserDao mapper = sqlsession.getMapper(UserDao.class); List<User> userList = mapper.getUserList();*/ //方式2 List<User> userList = sqlsession.selectList("com.chang.dao.UserDao.getUserList"); for (User user : userList) { System.out.println(user); } sqlsession.close(); } }
9.遇到的问题
(1)Mybatis核心配置文件 mybatis-config.xml 中的url可能格式正确但也链接不上 可以尝试 把&链接的部分调换一下顺序 建议把useSSL=false/true 放在最后
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8& serverTimezone=UTC&useSSL=false"/>
(2)这里Mapper注册 有时放在dao文件 与 接口类放在一起可能识别不到 ,此时可以把这个要注册的Mapper文件放在 resources文件下与mybatis-config.xml放在一起
<mappers> <mapper resource="UserMapper.xml"/> </mappers>
(3)在Mapper文件中 这几个部分要仔细 com.chang.dao.UserDa表示要实现的接口的包名
getUserList表示要实现的方法 resultType="com.chang.pojo.User" 表示要返回的数据类型
<mapper namespace="com.chang.dao.UserDao"> <select id="getUserList" resultType="com.chang.pojo.User"> select * from user </select> </mapper>