1- MyBatis简介
1.1-是什么
是apache的一个开源持久层框架,会托管JDBC,进行一些数据库的操作
获取MyBatis
1.2-持久化
数据持久化
持久化就是将数局在持久和瞬时转化的过程,也就是从内存-数据库转化的过程。
1.3-持久层
1.4-Why MyBatis?
- JDBC代码复杂。
- 框架方便自动化
- 用的人多…
- 其实不用也可以
2-Hello MyBatis
2.1-搭建环境
2.1.1-搭建数据库
CREATE TABLE `user`(
`id` INT PRIMARY KEY,
`name` VARCHAR(30) DEFAULT NULL,
`password` VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=UTF8;
INSERT INTO `user`VALUES
(1,'1','1'),
(2,'2','2'),
(3,'3','3')
SELECT * FROM `user`
2.1.2- 新建项目
maven依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
2.1.3-MyBatis核心配置文件
也就是指示如何连接数据库
dataSource中是具体连接什么数据库
environments可以加入多个environment
<?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="ConnectionString"/>
<property name="username" value="UserName"/>
<property name="password" value="PassWord"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
2.2-编码部分
2.2.1-编写mybatis工具类
public class MyBatisUtil {
static SqlSessionFactory sqlSessionFactory;
static{
try {
String resourse = "MyBatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resourse);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
2.2.2-编写实体类
同数据库表对应
package com.sunspot.dao;
public class User {
public int id;
public String name;
public String password;
}
2.2.3-编写mapper
对应对于一张表的全部操作
对应xxxxDao
package com.sunspot.dao;
import java.util.List;
public interface UserMapper {
public List<User> getUser();
}
2.2.5-编写mapper的xml
对应xxxxDaoImpl
这里namespace是作用类,id是函数名,resultType是返回类型,值是sql语句
<?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.sunspot.dao.UserMapper">
<select id="getUser" resultType="com.sunspot.dao.User" >
select * from MyBatisLearing.user
</select>
</mapper>
2.2.6-更改MyBatis-config.xml
这里要把之前编写的mapper添加到MyBatis-config.xml里。
<mappers>
<mapper resource="com/sunspot/dao/UserMapper.xml"/>
</mappers>
2.2.7-注意maven
maven没有学习,但是大概是maven有个过滤机制,只有满足条件的才会放在最后的项目里,这里我们需要让他将我们写的xxxmapper.xml加入到最后的项目中,
pom.xml加入如下内容
<mappers>
<mapper resource="com/sunspot/dao/UserMapper.xml"/>
</mappers>
2.3-测试
使用SqlSession.getMapper方法,参数为mapper的接口,返回一个接口,可以通过这个接口进行操作了
看起来,他是利用xxxmapper.xml的配置生成了一个实现接口的实现类,之后将实行类返回了。
public class UserMapperTest {
@Test
public void test1(){
MyBatisUtil myBatisUtil = new MyBatisUtil();
SqlSession sqlSession = myBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> user = mapper.getUser();
for (User user1 : user) {
System.out.println(user1);
}
sqlSession.close();
}
}
3- CRUD
3.1-接口
public interface UserMapper {
public List<User> getUserList();
public User getUser(int id);
public int deleteUser(int id);
public int updateUser(User user);
public int insertUser(User user);
}
3.2-Mapper.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">
<mapper namespace="com.sunspot.dao.UserMapper">
<select id="getUserList" resultType="com.sunspot.dao.User" >
select * from MyBatisLearing.user
</select>
<select id="getUser" resultType="com.sunspot.dao.User" parameterType="int">
select * from MyBatisLearing.user where id=#{id}
</select>
<insert id="insertUser" parameterType="com.sunspot.dao.User">
insert into MyBatisLearing.user(id, name, password) VALUES (#{id},#{name},#{password})
</insert>
<update id="updateUser" parameterType="com.sunspot.dao.User">
update MyBatisLearing.user set name=#{name},password=#{password} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from MyBatisLearing.user where id=#{id}
</delete>
</mapper>
3.3-测试(具体使用)
public class UserMapperTest {
@Test
public void getUserList(){
MyBatisUtil myBatisUtil = new MyBatisUtil();
SqlSession sqlSession = myBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> user = mapper.getUserList();
for (User user1 : user) {
System.out.println(user1);
}
sqlSession.close();
}
@Test
public void getUser(){
MyBatisUtil myBatisUtil = new MyBatisUtil();
SqlSession sqlSession = myBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user1 = mapper.getUser(1);
System.out.println(user1);
}
@Test
public void insertUser(){
MyBatisUtil myBatisUtil = new MyBatisUtil();
SqlSession sqlSession = myBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.insertUser(new User(11,"asdasd","wwwwww"));
System.out.println(i);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
MyBatisUtil myBatisUtil = new MyBatisUtil();
SqlSession sqlSession = myBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.deleteUser(11);
System.out.println(i);
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
MyBatisUtil myBatisUtil = new MyBatisUtil();
SqlSession sqlSession = myBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.updateUser(new User(2,"222","222"));
System.out.println(i);
sqlSession.commit();
sqlSession.close();
}
}
|