快速入门
相关API
Resources
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactoryBuilder
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSessionFactory
SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession
List<Student> list = sqlSession.selectList("StudentMapper.selectAll");
小结
映射配置文件
mapper.xml
.xml <select id="selectById" resultType="com.itheima.bean.Student" parameterType="java.lang.Integer"> SELECT * FROM student WHERE id = #{id} </select>
@Test public void selectById() throws IOException { //1.加载核心配置文件 //2.获取SqlSession工厂对象 //3.通过SqlSession工厂对象获取SqlSession对象 //4.执行映射配置文件中的sql语句,并接收结果 //5.处理结果 //6.释放资源 InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(); Student student = sqlSession.selectOne("StudentMapper.selectById", 3); System.out.println(student); sqlSession.close(); is.close(); }
<insert id="insert" parameterType="com.itheima.bean.Student"> INSERT student VALUES(#{id},#{name},#{age}); </insert>
核心配置文件
介绍
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束--> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--configuration 核心根标签--> <configuration> <!--引入数据库连接的配置文件--> <properties resource="jdbc.properties"/> <!--配置LOG4J--> <settings> <setting name="logImpl" value="log4j"/> </settings> <!--起别名--> <typeAliases> <typeAlias type="com.itheima.bean.Student" alias="student"/> <!--<package name="com.itheima.bean"/>--> </typeAliases> <!--environments配置数据库环境,环境可以有多个。default属性指定使用的是哪个--> <environments default="mysql"> <!--environment配置数据库环境 id属性唯一标识--> <environment id="mysql"> <!-- transactionManager事务管理。 type属性,采用JDBC默认的事务--> <transactionManager type="JDBC"></transactionManager> <!-- dataSource数据源信息 type属性 连接池--> <dataSource type="POOLED"> <!-- property获取数据库连接的配置信息 --> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <!-- mappers引入映射配置文件 --> <mappers> <!-- mapper 引入指定的映射配置文件 resource属性指定映射配置文件的名称 --> <mapper resource="StudentMapper.xml"/> </mappers> </configuration>
起别名
<!--起别名--> <typeAliases> <typeAlias type="com.itheima.bean.Student" alias="student"/> <!--<package name="com.itheima.bean"/>--> </typeAliases>
LOG4J
小结
接口代理方式
动态Sql
<select id="selectCondition" resultType="com.itheima.bean.Student" parameterType="com.itheima.bean.Student"> <!-- select * from student where id = ? and age = ? and name =?--> <include refid="select" /> <where> <if test="id != null"> id = #{id} </if> <if test="age != null"> AND age = #{age} </if> <if test="name !=null"> AND name = #{name} </if> </where> </select>
if&foreach
<select id="selectByIds" parameterType="list" resultType="com.itheima.bean.Student"> /*select * from student where id in (1,2,3)*/ <include refid="select"/> <where> <foreach collection="list" open="id in (" close=")" item="id" separator=","> #{id} </foreach> </where> </select>
sql片段抽取
分页插件介绍
实现步骤
<!-- .xml 集成分页插件--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>
//java //通过分页助手来实现 //pageNum 第几页,pageSize 显示几条数据 PageHelper.startPage(2,2);
相关参数
//获取分页相关参数 PageInfo<Student> info = new PageInfo<>(students); System.out.println("总条数:" + info.getTotal()); System.out.println("总页数:" + info.getPages()); System.out.println("当前页:" + info.getPageNum()); System.out.println("每页显示条数:" + info.getPageSize()); System.out.println("上一页:" + info.getPrePage()); System.out.println("下一页:" + info.getNextPage()); System.out.println("是否是第一页:" + info.isIsFirstPage()); System.out.println("是否是最后一页:" + info.isIsLastPage());
小结
多表操作
介绍
一对一
<mapper namespace="com.itheima.table01.OneToOneMapper"> <!--配置字段和实体对象属性的映射关系--> <resultMap id="oneToOne" type="card"> <id column="cid" property="id" /> <result column="number" property="number" /> <!-- association:配置被包含对象的映射关系 property:被包含对象的变量名 javaType:被包含对象的数据类型 --> <association property="p" javaType="person"> <id column="pid" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </association> </resultMap> <select id="selectAll" resultMap="oneToOne"> SELECT c.id cid,number,pid,NAME,age FROM card c,person p WHERE c.pid=p.id </select> </mapper>
一对多
<mapper namespace="com.itheima.table02.OneToManyMapper"> <resultMap id="oneToMany" type="classes"> <id column="cid" property="id"/> <result column="cname" property="name"/> <!-- collection:配置被包含的集合对象映射关系 property:被包含对象的变量名 ofType:被包含对象的实际数据类型 --> <collection property="students" ofType="student"> <id column="sid" property="id"/> <result column="sname" property="name"/> <result column="sage" property="age"/> </collection> </resultMap> <select id="selectAll" resultMap="oneToMany"> SELECT c.id cid,c.name cname,s.id sid,s.name sname,s.age sage FROM classes c,student s WHERE c.id=s.cid </select> </mapper>
多对多
public interface ManyToManyMapper { public abstract List<Student> selectAll(); }
public void selectAll() throws IOException { InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); ManyToManyMapper mapper = sqlSession.getMapper(ManyToManyMapper.class); List<Student> students = mapper.selectAll(); for (Student student : students) { System.out.println(student.getId()+","+student.getName()+","+student.getAge()); List<Course> courses = student.getCourses(); for (Course course : courses) { System.out.println(course.toString()); } } sqlSession.close(); is.close(); }
<mapper namespace="com.itheima.table03.ManyToManyMapper"> <resultMap id="manyToMany" type="Student"> <id column="sid" property="id"/> <result column="sname" property="name"/> <result column="sage" property="age"/> <collection property="courses" ofType="Course"> <id column="cid" property="id"/> <result column="cname" property="name"/> </collection> </resultMap> <select id="selectAll" resultMap="manyToMany" > SELECT s.id sid,s.name sname,s.age sage,c.id cid,c.name cname FROM student s,course c,stu_cr sc WHERE s.id = sc.sid AND c.id = sc.cid </select> </mapper>
小结
注解开发
SQL构建对象介绍
增删改查
public interface StudentMapper { @SelectProvider(type = ResultSql.class,method = "selectAll") public abstract List<Student> selectAll(); @InsertProvider(type = ResultSql.class,method = "insert") public abstract Integer insert(Student stu); @UpdateProvider(type = ResultSql.class,method = "update") public abstract Integer update(Student stu); @DeleteProvider(type = ResultSql.class,method = "delete") public abstract Integer delete(Integer id); }
public class ResultSql { public String selectAll() { return new SQL() { { SELECT("*"); FROM("student"); } }.toString(); } public String insert(Student stu) { return new SQL() { { INSERT_INTO("student"); INTO_VALUES("#{id},#{name},#{age}"); } }.toString(); } public String update(Student stu) { return new SQL() { { UPDATE("student"); SET("name=#{name}", "age=#{age}"); WHERE("id = #{id}"); } }.toString(); } public String delete(Integer id) { return new SQL() { { DELETE_FROM("student"); WHERE("id = #{id}"); } }.toString(); } }