一、创建maven项目并添加如下依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
二、创建mybatis_study数据库和t_user表用于测试
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
`address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `t_user` VALUES (1, '李洪刚', 20, '江阳区嘉裕花园3栋四楼15#');
INSERT INTO `t_user` VALUES (2, '王云华', 30, '纳溪区大渡镇红鱼村三大队');
INSERT INTO `t_user` VALUES (3, '郑小翠', 21, '江阳区老窖花园2栋五楼15号');
SET FOREIGN_KEY_CHECKS = 1;
三、编写User实体类用于数据封装
package com.dc.domain;
public class User {
private Integer id;
private String name;
private Integer age;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
四、编写dao层数据访问接口
package com.dc.dao;
import com.dc.domain.User;
import java.util.List;
public interface UserDao {
List<User> findAll();
User findUserById(Integer id);
}
五、resources中创建mysql连接文件jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_study?characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=1234
六、resources中创建mybatis主配置文件sqlMapConfig.xml
<configuration>
<properties resource="jdbc.properties">properties>
<typeAliases>
<package name="com.dc.domain"/>
typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.dc.dao"/>
mappers>
configuration>
七、创建UserDao的映射文件
<mapper namespace="com.dc.dao.UserDao">
<select id="findAll" resultType="user">
select *
from t_user
select>
<select id="findUserById" resultType="User" parameterType="Integer">
select *
from t_user
where id = #{id}
select>
mapper>
八、编写测试类进行测试
package com.dc;
import com.dc.dao.UserDao;
import com.dc.domain.User;
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.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
private InputStream in;
private SqlSession sqlSession;
private UserDao userDao;
@Before
public void init() throws Exception {
in = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession(true);
userDao = sqlSession.getMapper(UserDao.class);
}
@After
public void destroy() throws Exception {
sqlSession.close();
in.close();
}
@Test
public void testFindAll() {
List<User> users = userDao.findAll();
users.forEach(System.out::println);
}
@Test
public void testFindUserById() {
User user = userDao.findUserById(1);
System.out.println(user);
}
}
项目结构:
测试结果: