大家可能都在使用ssm ssm中的m指的是mybatis(以前叫ibatis),今天我们一起学习一下mybaits。
mybaits有三种方式
第一种不使用mapper 接口
第二种使用mapper 接口
第三种使用注解的方法
我们一一说起
第一种不适用mapper 接口的方法
使用xml
步骤一:在使用的时候我们需要进行导包操作,添加mybatis 所需要的依赖包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
步骤二:我们开始编写mybaits.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节点
里面有配置信息 分别是环境和映射
其中环境里有datasource,里面有我们熟悉的连接数据库的四个字符串
-->
<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/demo"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--
连锁上要查找的语句包
-->
<mappers>
<mapper resource="com/zmz/entity/UserMapper.xml"></mapper>
</mappers>
</configuration>
步骤三:创建bean 类
package com.zmz.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor //有参构造
@NoArgsConstructor //无参构造
public class User {
private int uid;
private String name;
private String addr;
}
这里给大家推荐一个管理bean 的插件(lombok,本人感觉是非常的好用!!!)
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
此插件还要再下载和重启idea
步骤四:进行数据库语言的编写
<?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 填写要动态代理的类的地址-->
<mapper namespace="test">
<!--
查询所有
-->
<select id="getUser" resultType="com.zmz.entity.User">
select * from t_user
</select>
<!--
添加信息
-->
<insert id="addUser" parameterType="com.zmz.entity.User">
insert into t_user values (#{uid},#{name},#{addr})
</insert>
<!--
修改信息
-->
<update id="updateUser" parameterType="java.util.Map">
update t_user set name=#{name} where uid=#{uid}
</update>
<!--
删除信息
-->
<delete id="deletUser" parameterType="int">
delete from t_user where uid=#{uid}
</delete>
</mapper>
步骤五:进行数据库的操作
package com.zmz.entity;
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.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
public class UserTest {
/**
* 查询所有
*/
@Test
public void testUserMapper(){
try {
String resource = "mybatis.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sf.openSession(true);
List<User> user = sqlSession.selectList("test.getUser");
System.out.println(user);
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 添加操作
*/
@Test
public void testUserMapperInsert(){
User user = new User(4,"littleBoy","杭州");
try {
String resrource = "mybatis.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resrource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sf.openSession(true);
int insert = sqlSession.insert("test.addUser",user);
System.out.println(insert);
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 修改操作
*/
@Test
public void testUserMapperUpdate(){
Map<String,Object> map = new HashMap<>();
map.put("name","青檬");
map.put("uid",1);
try {
String resource = "mybatis.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sf.openSession();
int update = sqlSession.update("test.updateUser", map);
System.out.println(update);
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除操作
*/
@Test
public void testUserMapperDelete(){
try {
String resource = "mybatis.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sf.openSession(true);
int delete = sqlSession.delete("test.deletUser", 4);
System.out.println(delete);
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
看完代码我们不难发现,在第五步的代码中存在多处相同的地方我们一起来看一下
String resource = "mybatis.xml";
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sf.openSession(true);
这些其实都是mybatis统一的操作,创建工厂类,打开工厂,
而在最后的处理的时候
涉及到了事务的提交,在使用DML(数据操纵语言)语言的时候要进行数据的提交操作,不然数据库不会显示