MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
<configuration>
<typeAliases>
<package name="hu.mybatis.pojo"/>
typeAliases>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=Asia/Shanghai&useSSL=false"/>
<property name="username" value="test"/>
<property name="password" value="123"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="hu/mybatis/mapper/StudentMapper.xml"/>
mappers>
configuration>
<typeAliases>
<typeAlias alias="Author" type="domain.blog.Author"/>
typeAliases>
<typeAliases>
<package name="hu.mybatis.pojo"/>
typeAliases>
@Alias("author")
public class Author {
...
}
完成全局配置后,就可以定义SQL语句,但需要加载SQL语句的映射文件,可以通过全局配置中的映射器来加载SQL语句的映射文件。
相对于类路径的资源引用
<mappers>
<mapper resource="hu/mybatis/mapper/StudentMapper.xml"/>
mappers>
亦可以通过包内映射器接口实现全部注册为映射器
<mappers>
<package name="org.mybatis.builder"/>
mappers>
<mapper namespace="hu.mybatis.mapper.StudentMapper">
<select id="selTea" parameterType="int" resultType="teacher">
select * from teacher where id=#{0}
select>
mapper>
//加载全局配置文件并解析
InputStream is = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
SqlSession session=factory.openSession();
//查询数据库中teacher表
List<Student> list=session.selectList("hu.mybatis.mapper.StudentMapper.selStu");
System.out.println(list.toString());
PeopleMapper.xml文件中
<mapper namespace="hu.mybatis.mapper.PeopleMapper">
<select id="selByNameAndId" resultType="People">
select * from people where id=#{param1} and name=#{param2}
select>
mapper>
新建PeopleMapper.java文件
package hu.mybatis.mapper;
import hu.mybatis.pojo.People;
//接口名要与PeopleMapper.xml文件中的一致(该接口位于hu.mybatis.mapper包下)
public interface PeopleMapper {
//方法名与PeopleMapper.xml中的查询SQL映射的id值一致
People selByNameAndId(int id,String name);
}
调用方式为:
InputStream is = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
SqlSession session=factory.openSession();
PeopleMapper pm=session.getMapper(PeopleMapper.class);
People people = pm.selByNameAndId(3, "张三");
System.out.println(people);
<resultMap type="teacher" id="mymap">
<id column="id" property="id1" />
<result column="name" property="name1"/>
resultMap>
<select id="selAll" resultMap="mymap">
select * from teacher
select>
N+1 查询方式,先查询出某个表的全部信息,根据这个表的信息查询另一个表的信息.
<select id="selById" resultType="teacher" parameterType="int">
select * from teacher where id=#{0}
select>
<resultMap type="student" id="stuMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="tid" column="tid"/>
<association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid">
association>
resultMap>
<select id="selAll" resultMap="stuMap">
select * from student
select>
提使用 N+1 方式.时如果列名和属性名相同可以不配置,使用 Automapping 特性.但是 mybatis 默认只会给列专配一次,因此在StudentMapper可以简化配置
<resultMap type="student" id="stuMap">
<result property="tid" column="tid"/>
<association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid">
association>
resultMap>
<select id="selAll" resultMap="stuMap">
select * from student
select>
简化SQL映射的配置文件
@Select("select * from teacher")
List<Teacher> selAll();
@Insert("insert into teacher values(default,#{name})")
int insTeacher(Teacher teacher);
@Update("update teacher set name=#{name} where id=#{id}")
int updTeacher(Teacher teacher);
@Delete("delete from teacher where id=#{0}")
int delById(int id);
@Results(value={
@Result(id=true,property="id",column="id"),
@Result(property="name",column="name"),
@Result(property="list",column="id",many=
@Many(select="com.bjsxt.mapper.StudentMapper.selByTid"))
})
@Select("select * from teacher")
List<Teacher> selTeacher();
文件中:
<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=Asia/Shanghai&useSSL=false" />
报错:The reference to entity “useSSL” must end with the ‘;’ delimiter.
文件中将&改成分号,如下:
<property name="url" value="jdbc:mysql://localhost:3306/ssm?
serverTimezone=Asia/Shanghai;useSSL=false" />
报错: No timezone mapping entry for ‘Asia/Shanghai;useSSL=false’
更改后,xml中url配置如下:
<property name="url" value="jdbc:mysql://localhost:3306/ssm?
serverTimezone=Asia/Shanghai&useSSL=false" />
结果:正常运行