Mybatis概述: 一个持久层框架
主体架构:
SqlMapperConfig.xml(核心配置文件) : sqlSessionFactory 和 sqlsession
↓
Mapper.xml(动态sql的语句实现)
↓
MappedStatement(方便动态加载Sql,用于绑定参数和返回值的封装)
↓
DB(数据库)
<?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">
<!-- 事务管理 :默认的jdbc事务,取消了单条sql的自动提交 -->
<transactionManager type="JDBC"/>
<!-- 数据源的配置 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_gm"/>
<property name="username" value="root"/>
<property name="password" value="000000"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--
单独采用mapper文件实现数据库操作
<mapper resource="com/zhongruan/mapper/StudentMapper.xml"/>-->
<!--需要根据引用接口的形式进行调用-->
<mapper class="com.zhongruan.dao.StudentDao"/>
<mapper class="com.zhongruan.dao.UserDao"/>
</mappers>
</configuration>
三: bean层的编写(与mysql数据库的数据进行对应)
package com.zhongruan.bean;
public class Student {
private int id;
private String stuName;
private String stuSex;
private String stuAge;
public Student(int id, String stuName, String stuSex, String stuAge) {
this.id = id;
this.stuName = stuName;
this.stuSex = stuSex;
this.stuAge = stuAge;
}
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public String getStuAge() {
return stuAge;
}
public void setStuAge(String stuAge) {
this.stuAge = stuAge;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", stuName='" + stuName + '\'' +
", stusex='" + stuSex + '\'' +
", stuage='" + stuAge + '\'' +
'}';
}
}
四: 进行dao层编写
package com.zhongruan.dao;
import com.zhongruan.bean.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface StudentDao {
//对学生类的 增删改查
int addStu(Student student);
int delStu(Integer id);
int updStu(Student student);
Student getStuById(Integer id);
//测试参数绑定
int addStu2(@Param("a") int id,@Param("b") String name,@Param("c") String sex,@Param("d") String age);
int addStu3(List stuInfo);
int addStu4(Map map);
}
五: 进行sql语句的编写
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 给mapper绑定接口 -->
<mapper namespace="com.zhongruan.dao.StudentDao">
<!-- 测试增加
动态sql:id值 就是sql的引用名
参数绑定:1.需要制定参数类型
-->
<insert id="addStu" parameterType="com.zhongruan.bean.Student">
INSERT INTO tb_student (id,name,sex,age) VALUES (#{
id},#{
stuName},#{
stuSex},#{
stuAge})
</insert>
<delete id="delStu" parameterType="Integer">
DELETE FROM tb_student where id=#{
id}
</delete>
<update id="updStu" parameterType="com.zhongruan.bean.Student">
UPDATE tb_student SET name=#{
stuName},sex=#{
stuSex},age=#{
stuAge} WHERE id=#{
id}
</update>
<select id="getStuById" parameterType="Integer" resultType="com.zhongruan.bean.Student">
SELECT id,name as stuName,sex as stuSex,age as stuAge
from tb_student WHERE id=#{
id}
</select>
<select id="getStuAll" resultType="com.zhongruan.bean.Student">
SELECT * FROM tb_student
</select>
<!-- Mapper中 动态sql中各种参数形式的绑定传递
1.基本类型:单个 或者 多个
单个参数的传递声明:parameterType="java.lang.Integer"
多个参数的传递声明:ibatis 注解, @Param
2.引用类型: 单个对象 或者 多个对象
单个对象的声明:parameterType="com.zhongruan.bean.Student"
多个对象的声明也可以使用注解
3.数组或者集合:
单个数组,单个List, Map
如果传参是数组:array
如果传参是集合:list
-->
<insert id="addStu2">
INSERT INTO tb_student (id,name,sex,age) VALUES (#{
a},#{
b},#{
c},#{
d})
</insert>
<insert id="addStu3" parameterType="java.util.ArrayList">
INSERT INTO tb_student (id,name,sex,age)
VALUES (
<foreach collection="list" item="item1" separator=",">
#{
item1}
</foreach>
);
</insert>
<insert id="addStu4" parameterType="java.util.Map">
INSERT INTO tb_student (id,name,sex,age)
VALUES (
#{
id},#{
name},#{
sex},#{
age}
);
</insert>
</mapper>
六: 进行测试:
package com.zhongruan.test;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.zhongruan.dao.StudentDao;
import com.zhongruan.bean.Student;
public class Test3 {
@Test
public void test1() throws IOException {
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
InputStream resource = Resources.getResourceAsStream("sqlMapperConfig.xml");
SqlSessionFactory sessionFactory=builder.build(resource);
SqlSession sqlSession=sessionFactory.openSession();
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
// Student student=new Student();
// student.setId(4);
// student.setStuName("老王");
// student.setStuSex("男");
// student.setStuAge("28");
// int i=studentDao.addStu(student);
int i=studentDao.delStu(4);
sqlSession.commit();
sqlSession.close();
System.out.println(i);
}
@Test
public void test2() throws IOException {
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
InputStream resource = Resources.getResourceAsStream("sqlMapperConfig.xml");
SqlSessionFactory sessionFactory=builder.build(resource);
SqlSession sqlSession=sessionFactory.openSession();
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
// int i=studentDao.addStu2(4,"老刘","女","21");
// List stuInfo =new ArrayList();
// stuInfo.add("5");
// stuInfo.add("如花");
// stuInfo.add("女");
// stuInfo.add("21");
Map map=new HashMap();
map.put("id",6);
map.put("name","妖姬");
map.put("sex","女");
map.put("age","20");
int i=studentDao.addStu4(map);
sqlSession.commit();
sqlSession.close();
System.out.println(i);
}
}
总结:mybatis在JDBC的基础上进行完美的封装,保留了JDBC动态sql的编写,优化了参数的绑定和返回指的封装,进行编写时,要注意dao层的方法名要与对应ql语句的id相对应,并且在编写sql语句时,也要注意此方法是否有返回值,是什么类型的,不同类型要使用不同的语句。