数据准备
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`username` varchar(30) NOT NULL,
`sex` varchar(1) NOT NULL,
`birthday` varchar(10) NOT NULL,
`address` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into user values(1,"steven","1","1991-02-11","nanjing");
insert into user values(2,"sherry","0","1995-06-14","taizhou");
insert into user values(3,"steven007","1","1995-01-01","nanjing");
insert into user values(4,"sherry007","0","1995-12-31","taizhou");
搭建MyBatis工程
1.使用IDEA创建工程
2.引入项目使用的依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
3.Mapper接口开发方式实现原理
Mapper接口开发方式只需要程序员编写Mapper接口即可,由Mybatis框架根据接口定义创建接口的动态代理对象。Mapper接口开发需要遵循以下规范:
- Mapper.xml文件中的namespace与Mapper接口的类路径相同。
- Mapper.xml中定义的statement的id与Mapper接口方法名相同 。
- Mapper.xml中定义的SQL的parameterType与Mapper接口方法的输入参数类型相同。
- Mapper.xml中定义的SQL的resultType与Mapper接口方法的输出参数类型相同。
4.编写MyBatis框架全局配置文件mybatis.xml
在项目目录“/src/main/resources”下新建config目录,并在config目录下新建mybatis.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>
<!-- 1.环境配置,和spring整合后environments配置将废除 -->
<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/study?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="admin123" />
</dataSource>
</environment>
</environments>
<!-- 2.在全局配置文件中加载SQL映射文件 -->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
5.编写po类
在项目目录“/src/main/java/com/steven”下新建po(persistent object)目录,并在po目录下新建User类,具体代码如下。
public class User {
//属性名要和数据库表的字段对应
private Integer id;
private String username;
private String sex;
private String birthday;
private String address;
public User() {
}
public User(Integer id, String username, String sex, String birthday, String address) {
this.id = id;
this.username = username;
this.sex = sex;
this.birthday = birthday;
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
", birthday='" + birthday + '\'' +
", address='" + address + '\'' +
'}';
}
//get和set方法......
}
6.编写Mapper接口
在项目目录“/src/main/java/com/steven”下新建mapper目录,并在mapper目录下新建UserMapper接口,具体代码如下。
public interface UserMapper {
User findUserById(Integer id);
}
7.编写SQL映射
在项目目录“/src/main/resources”下新建mapper目录,并在mapper目录下新建UserMapper.xml文件,具体代码如下。
<?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">
<!-- namespace的作用是对SQL进行分类管理,使用mapper代理方式开发,namespace的值是该文件的全限定名 -->
<mapper namespace="com.steven.mapper.UserMapper">
<!-- 在映射文件中配置多个SQL语句 -->
<!--
id:标识映射文件中的SQL,对应Mapper接口类的方法名
parameterType:指定输入参数的类型,对应Mapper接口类的方法入参类型
resultType:指定SQL输出结果映射的Java对象类型
#{}表示一个占位符,#{value}其中value表示接收输入参数的名称,如果输入参数是简单类型,那么#{}中的值可以任意。
-->
<!-- 根据用户id查找用户 -->
<select id="findUserById" resultType="com.steven.po.User" parameterType="java.lang.Integer">
select * from user
where id=#{id}
</select>
</mapper>
8.编写测试类
在项目目录“/src/test/java”下新建Test类,具体代码如下。
public class Test {
public static void main(String[] args) throws Exception {
//1.获取MyBatis全局配置文件流
String resource = "config/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//2.根据MyBatis配置信息创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.获取会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.获取Mapper
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//5.业务操作
User user = userMapper.findUserById(1);
System.out.println(user);
sqlSession.close();
}
}
User{id=1, username='steven', sex='1', birthday='1991-02-11', address='nanjing'}
9.工程目录