一、MyBatis相关概念回顾
1.对象/关系数据库映射(ORM)
ORM 全称Object Relation Mapping : 表示对象-关系映射的缩写
ORM 完成面向对象的编程语言到关系数据库的映射,它把关系数据库包装成面向对象的模型。采用ORM框架后,应用程序不再直接访问底层数据库,而是以面向对象的方式来操作持久化对象,ORM将把这些对象的操作转换成底层SQL操作。
2.MyBatis简介
- MyBatis是一款优秀的基于ORM的半自动的轻量级的持久层框架,它支持定制化SQL以及高级映射。它避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。
- MyBatis可以使用简单的XML或者注解来配置和映射原生类型、接口和Java的POJO成数据库中的记录。
3.MyBatis历史
- 它原是apache的一个开源项目iBatis,于2010年6月由apache迁移到了google,iBatis3.x正式更名为MyBatis,代码于2013年11月迁移到GitHub。
- iBatis一词来源于"internet"和"abatis"的组合,是一个基于JAVA的持久层框架。它包括SQL Maps和Data Access Objects(DAO)
4.MyBatis优势
- MyBatis是一个半自动的持久层框架,对于开发人员来说,核心的sql还是需要自己进行优化。
- sql和java的编码进行了分离,功能边界更清晰,一个专注业务,一个专注数据。
- 全自动框架生成的sql不易进行优化。当bean字段很多时,无法做到只映射部分字段。
二、MyBatis环境搭建回顾
1.开发步骤
- 引入myBatis的maven坐标
- 创建数据库表
- 创建实体类
- 编写映射文件xxxMapper.xml
- 编写核心配置文件sqlMapConfig.xml
2.环境搭建
1.创建一个maven项目
并添加pom.xml文件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.w</groupId>
<artifactId>mybatis_persistence</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
2.创建数据库并添加数据
create table user
(
id int null,
username varchar(20) default ''
);
insert into user (id,username) values (1,'zhangsan');
insert into user (id,username) values (2,'lisi');
3.创建实体类
package com.w.pojo;
public class User {
private Integer id;
private String name;
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
4.编写UserMapper.xml
<mapper namespace="com.w.dao.UserDao">
<select id="findAll" resultType="com.w.pojo.User">
select * from user;
</select>
<select id="findOne" parameterType="com.w.pojo.User" resultType="com.w.pojo.User">
select * from user where id =#{id}
</select>
<insert id="insertUser" parameterType="com.w.pojo.User">
insert into user values(#{id},#{name})
</insert>
<insert id="updateUser" parameterType="com.w.pojo.User">
update user set name=#{name} where id=#{id}
</insert>
<delete id="deleteUserById" parameterType="com.w.pojo.User">
delete from user where id=#{id}
</delete>
</mapper>
5.编写sqlMapConfig.xml
<config>
<dataSource>
<properties name="jdbcUrl" value="jdbc:mysql://localhost:3306/test1"></properties>
<properties name="driver" value="com.mysql.jdbc.Driver"></properties>
<properties name="name" value="root"></properties>
<properties name="password" value="123456"></properties>
</dataSource>
<mappers>
<mapper resource="UserMapper.xml"></mapper>
</mappers>
</config>
6.编写测试类
package com.w;
import com.w.dao.UserDao;
import com.w.pojo.User;
import com.w.session.SessionFactory;
import com.w.session.SessionFactoryBuilder;
import com.w.io.Resource;
import com.w.session.SqlSession;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class TestDemo {
/**
* getMapper方式查询delete
*/
@Test
public void testDelete() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream("sqlMapConfig.xml"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(5);
//inputUser.setName("tom5");
UserDao userDao = sqlSession.getMapper(UserDao.class);
userDao.deleteUserById(inputUser);
}
/**
* getMapper方式查询update
*/
@Test
public void testUpdate() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream("sqlMapConfig.xml"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(5);
inputUser.setName("tom5");
UserDao userDao = sqlSession.getMapper(UserDao.class);
userDao.updateUser(inputUser);
}
/**
* getMapper方式查询insert
*/
@Test
public void testInsertUserByMapper() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream("sqlMapConfig.xml"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(5);
inputUser.setName("tom");
UserDao userDao = sqlSession.getMapper(UserDao.class);
userDao.insertUser(inputUser);
}
/**
* getMapper方式查询select
*/
@Test
public void testSelectListBygetMapper() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream("sqlMapConfig.xml"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(1);
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println(user);
}
}
/**
* getMapper方式查询selectOne
*/
@Test
public void testSelectOneBygetMapper() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream("sqlMapConfig.xml"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(1);
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.findOne(inputUser);
System.out.println(user);
}
/**
* select方法
*/
@Test
public void testSelectById() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream("sqlMapConfig.xml"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(1);
User user = sqlSession.selectOne("com.w.dao.UserDao.findOne", inputUser);
System.out.println(user);
}
/**
* 测试获取SessionFactoryBuilder
*/
@Test
public void testSessionFactoryBuilder() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream("sqlMapConfig.xml"));
SqlSession sqlSession = build.openSesssion();
List<User> users = sqlSession.selectList("com.w.dao.UserDao.findAll");
for (User user : users) {
System.out.println(user);
}
}
/**
* 测试获取输入流对象是否成功
*/
@Test
public void testGetResource(){
InputStream resource = Resource.getResourceAsStream("sqlMapConfig.xml");
System.out.println(resource);
}
/**
* 第一个测试类。。。
*/
@Test
public void test1(){
System.out.println("测试一下。。。");
}
}
执行查询所有,即完成mybatis基本查询了。