目录
实体类与表的映射问题
数据库库表的字段名称和实体类的属性名称不一样,则不能自动封装数据,实体类不能封装的字段为null
如数据库表的字段如下
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| ID | int | NO | PRI | NULL | |
| student_name | varchar(5) | YES | | NULL | |
| student_classID | varchar(13) | YES | | NULL | |
| student_gender | varchar(1) | YES | | NULL | |
| student_cardID | varchar(19) | YES | | NULL | |
| student_address | varchar(100) | YES | | NULL | |
| student_phone | varchar(13) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
实体类中的字段
public class Student{
private Integer ID;
private String name;
private String classID;
private String gender;
private String cardID;
private String address;
private String phone;
}
<?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="top.wogong.mapper.UserMapper">
<!--数据库中的表和实体类中字段不一样,不能自动封装-->
<select id="selectAll" resultType="top.wogong.pojo.User">
select * from StudentInformation;
</select>
</mapper>
解决实体类中的字段与表中字段不一样
取别名
<?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="top.wogong.mapper.UserMapper">
<!-- as取别名 -->
<select id="selectAll" resultType="top.wogong.pojo.User">
select
ID,
student_name as name,
student_classID as classID,
student_gender as gender,
student_cardID as cardID,
student_address as address,
student_phone as phone
from studentinformation;
</select>
</mapper>
使用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="top.wogong.mapper.UserMapper">
<!--使用sql片段-->
<sql id="brand_column">
ID,
student_name as name,
student_classID as classID,
student_gender as gender,
student_cardID as cardID,
student_address as address,
student_phone as phone
</sql>
<select id="selectAll" resultType="top.wogong.pojo.User">
select
<!--通过sql的id引用sql片段-->
<include refid="brand_column"/>
from studentinformation;
</select>
</mapper>
使用resultMap
<?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="top.wogong.mapper.StudentMapper">
<!--id 表示resultMap的标识 type为映射类型-->
<resultMap id="studentResultMap" type="top.wogong.pojo.Student">
<!--
完成主键映射
<id property="" column=""/>
完成普通字段的映射
<result property="" column=""/>
column是数据库表的列名 property是java实体类的属性名
-->
<result property="name" column="student_name"/>
<result property="classID" column="student_classID"/>
<result property="gender" column="student_gender"/>
<result property="cardID" column="student_cardID"/>
<result property="address" column="student_address"/>
<result property="phone" column="student_phone"/>
</resultMap>
<!--resultMap等于上面的resultMap id-->
<select id="selectAll" resultMap="studentResultMap">
select * from studentinformation;
</select>
</mapper>
mybatis接收参数
#{} :会将其替换为? 可以防止SQL注入
$() :拼SQL,存在SQL输入
使用时机:
参数传递的时候使用#{}
表名或者列名不固定的情况下使用$()
参数类型:parameterType:可以省略
特殊字符处理(xml标签的特殊字符):
1、转义字符:如:< 可以使用 <
2、CDATA区
<![CDATA[ < ]]>
在mapper接口方法中定义要传的参数
package top.wogong.mapper;
import top.wogong.pojo.Student;
import java.util.List;
//Mapper接口
public interface StudentMapper {
List<Student> selectAll();
#定义方法,传递到SQL中的参数参数
Student selectById(int id);
}
在映射文件中,编写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="top.wogong.mapper.StudentMapper">
<!--id 表示resultMap的标识 type为java中的实体类-->
<resultMap id="studentResultMap" type="top.wogong.pojo.Student">
<!--column是数据库中的字段 property是java实体类的字段-->
<result property="name" column="student_name"/>
<result property="classID" column="student_classID"/>
<result property="gender" column="student_gender"/>
<result property="cardID" column="student_cardID"/>
<result property="address" column="student_address"/>
<result property="phone" column="student_phone"/>
</resultMap>
<!--resultMap等于上面的resultMap id-->
<select id="selectAll" resultMap="studentResultMap">
select * from studentinformation;
</select>
<!--resultMap 表示输出类型 paramenterType表示传入的类-->
<!--编写SQL-->
<select id="selectById" resultMap="studentResultMap" parameterType="int">
<!--id 为selectById传过来的参数 -->
select * from studentinformation where ID=#{id}
</select>
</mapper>
多个参数传递
使用@Param()传递参数
接口中定义的方法
package top.wogong.mapper;
import org.apache.ibatis.annotations.Param;
import top.wogong.pojo.Student;
import java.util.List;
public interface StudentMapper {
List<Student> selectAll();
Student selectById(int id);
<!--使用@param("") 传递多个参数-->
List<Student> selectByCondition(@Param("address") String student_address,@Param("name") String student_name);
}
配置与接口对应的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="top.wogong.mapper.StudentMapper">
<!--id 表示resultMap的标识 type为java中的实体类-->
<resultMap id="studentResultMap" type="top.wogong.pojo.Student">
<!--column是数据库中的字段 property是java实体类的字段-->
<result property="name" column="student_name"/>
<result property="classID" column="student_classID"/>
<result property="gender" column="student_gender"/>
<result property="cardID" column="student_cardID"/>
<result property="address" column="student_address"/>
<result property="phone" column="student_phone"/>
</resultMap>
<!--resultMap等于上面的resultMap id-->
<select id="selectAll" resultMap="studentResultMap">
select * from studentinformation;
</select>
<!--resultMap 表示输出类型 paramenterType表示传入的类-->
<select id="selectById" resultMap="studentResultMap" parameterType="int">
select * from studentinformation where ID=#{id}
</select>
<select id="selectByCondition" resultMap="studentResultMap">
<!--
address 对应selectByCondition的@Param("address")String student_address
name 对应selectByCondition的@Param("name")String student_name
-->
select * from studentinformation where student_address like #{address} and student_name like#{name};
</select>
</mapper>
使用对象传递
定义类
package top.wogong.pojo;
public class AddressAndName {
private String name;
private String address;
public AddressAndName() {
}
public AddressAndName(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
定义接口中的方法
package top.wogong.mapper;
import org.apache.ibatis.annotations.Param;
import top.wogong.pojo.AddressAndName;
import top.wogong.pojo.Student;
import java.util.List;
public interface StudentMapper {
List<Student> selectAll();
Student selectById(int id);
//@Param()
List<Student> selectByCondition(@Param("address") String student_address,@Param("name") String student_name);
//以对象的方式传递
List<Student> selectByCondition(AddressAndName addressAndName);
}
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="top.wogong.mapper.StudentMapper">
<!--id 表示resultMap的标识 type为java中的实体类-->
<resultMap id="studentResultMap" type="top.wogong.pojo.Student">
<!--column是数据库中的字段 property是java实体类的字段-->
<result property="name" column="student_name"/>
<result property="classID" column="student_classID"/>
<result property="gender" column="student_gender"/>
<result property="cardID" column="student_cardID"/>
<result property="address" column="student_address"/>
<result property="phone" column="student_phone"/>
</resultMap>
<!--resultMap等于上面的resultMap id-->
<select id="selectAll" resultMap="studentResultMap">
select * from studentinformation;
</select>
<!--resultMap 表示输出类型 paramenterType表示传入的类-->
<select id="selectById" resultMap="studentResultMap" parameterType="int">
select * from studentinformation where ID=#{id}
</select>
<select id="selectByCondition" resultMap="studentResultMap">
select * from studentinformation where student_address like #{address} and student_name like#{name};
</select>
</mapper>
使用map集合
定义接口的方法
package top.wogong.mapper;
import org.apache.ibatis.annotations.Param;
import top.wogong.pojo.AddressAndName;
import top.wogong.pojo.Student;
import java.util.List;
import java.util.Map;
public interface StudentMapper {
List<Student> selectAll();
Student selectById(int id);
//@Param()
List<Student> selectByCondition(@Param("address") String student_address,@Param("name") String student_name);
//以对象的方式传递
List<Student> selectByCondition(AddressAndName addressAndName);
//使用Map集合传递参数
List<Student> selectByCondition(Map map);
}
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="top.wogong.mapper.StudentMapper">
<!--id 表示resultMap的标识 type为java中的实体类-->
<resultMap id="studentResultMap" type="top.wogong.pojo.Student">
<!--column是数据库中的字段 property是java实体类的字段-->
<result property="name" column="student_name"/>
<result property="classID" column="student_classID"/>
<result property="gender" column="student_gender"/>
<result property="cardID" column="student_cardID"/>
<result property="address" column="student_address"/>
<result property="phone" column="student_phone"/>
</resultMap>
<!--resultMap等于上面的resultMap id-->
<select id="selectAll" resultMap="studentResultMap">
select * from studentinformation;
</select>
<!--resultMap 表示输出类型 paramenterType表示传入的类-->
<select id="selectById" resultMap="studentResultMap" parameterType="int">
select * from studentinformation where ID=#{id}
</select>
<select id="selectByCondition" resultMap="studentResultMap">
select * from studentinformation where student_address like #{address} and student_name like#{name};
</select>
</mapper>
测试
public void test1() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取SqlSession对象,用来执行sql
SqlSession sqlSession=sqlSessionFactory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Map aan=new HashMap();
aan.put("name","%汪晓露%");
aan.put("address","%河池%");
List<Student> students = mapper.selectByCondition(aan);
System.out.println(students);
}
mybatis动态SQL
if标签
当test中的表达式为true时,会把if标签中的内容添加到SQL中
if 多条件的使用
语法
<select>
select 字段 from 表 where
<if test="表达式"></if>
<if test="表达式"></if>
</select>
<select>
select * from student
where
<!--test="表示表达"-->
<if test="name != null and name='' ">
<!--
当参数name不为null,并且name为空字符串时,将name=#{name}添加到where后
即 select *from student where name=#{name}
-->
name=#{name}
</if>
<if test="classid !=null ">
<!--
当参数class不为null,并且name也不为null和空字符串 将 and classid=#{classid}后面
即 selecct * from student where name={name} and classid=#{classid}
-->
and classid=#{classid}
</if>
</select>
where标签
用来替换SQL中的where,防止使用if标签时SQL中的where语句错误
上面例子中当name为null或空字符串时,会出现
select * from student where and classid=#{classid}
的SQL语法错误
有两种方法解决错误
1、恒等式发
<select>
<!--在where 后加 1=1 恒等式 -->
select * from student where 1=1
<if test="name !=name and name !=''">name=#{name}</if>
<if test="classid !=null">classid=#{classid}</if>
</select>
2、使用mydatis的<where></where>替换where
<select>
select * from student
<where>
<if test="name !=null and nume !='' "> name=#{name}</if>
<if test="classid !=null ">classid=#{classid}</if>
</where>
</select>
choose标签
choose(when,otherwise):类似于java中的switch
从多条件中选择一个choose
语法:
<select>
select * from 表
where
<choose> <!--类似于switch-->
<when test="表达式1">SQL语句</when><!--类似于 case-->
<when test="表达式2">SQL语句</when>
<when test="表达式3">SQL语句</when>
<otherwise></otherwise><!--类似default-->
</choose>
</select>
foreach标签
用于遍历数组与集合中的成员
<foreach collection="集合类型" item="集合中的成员" open="开始字符" close="结束字符" separator="集合之间的分隔符">
</foreach>
使用
接口
List<Teacher> selectReturnValue(List<Integer> list);
<select id="selectReturnValue" resultMap="resultMap">
select * from teacher where ttid in
<!--list为selectReturnValue的参数-->
<foreach collection="list" item="a" open="(" separator="," close=")">
#{a}
</foreach>
<!-- select * from teacher where ttid in (....) -->
</select>
一对一查询
使用
<association property="类中的属性" javaType="java的数据类型">
</association>
一对多查询
一对多查询使用collection标签
<collection property="数据类型" ofType="对应类类型">
</collection>
下面拿类老师和学生来举例
一个老师可以教几个学生
存在一对多的关系
teacher老师表的内容下
student学生表的内容如下
student类的定义如下
package top.wogong.mybatis.pojo;
public class Student {
private String sname;
private Integer id;
private Integer age;
private Integer tid;
public Student() {
}
public Student(String sname, Integer id, Integer age, Integer tid) {
this.sname = sname;
this.id = id;
this.age = age;
this.tid = tid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
@Override
public String toString() {
return "Student{" +
"sname='" + sname + '\'' +
", id=" + id +
", age=" + age +
", tid=" + tid +
'}';
}
}
teacher类定义如下
package top.wogong.mybatis.pojo;
import java.util.List;
public class Teacher {
private Integer ttid;
private String tname;
private String qq;
//多个学生
List<Student> students;
public Teacher() {
}
public Teacher(Integer ttid, String tname, String qq, List<Student> students) {
this.ttid = ttid;
this.tname = tname;
this.qq = qq;
this.students = students;
}
public Integer getTtid() {
return ttid;
}
public void setTtid(Integer ttid) {
this.ttid = ttid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher{" +
"ttid=" + ttid +
", tname='" + tname + '\'' +
", qq='" + qq + '\'' +
", students=" + students +
'}';
}
}
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="top.wogong.mybatis.mapper.UserMapper">
<!-- 通过resultMap指定表的属性值与实体类的的属性映射关系 -->
<!--
resultMap的
id:用于标记这个resultMap
type:用于指向映射的实体类
-->
<resultMap id="resultMap" type="top.wogong.mybatis.pojo.Teacher">
<!--
id标签 的
property:对应实体类定义的id,表示与数据库的column表示映射
column:对应数据库表的id
-->
<id property="ttid" column="ttid"/>
<!--
result标签的
property:对应实体类中的成员属性,表示与column映射
column:对应数据表中的字段名称
-->
<result property="tname" column="tname"/>
<result property="qq" column="qq"/>
<!--
collection用于一对多
collection标签的
property:对应实体类中的字段
ofType:对应property的数据类型
-->
<collection property="students" ofType="top.wogong.mybatis.pojo.Student">
<!--
collection中的内容表示 ofType所指向的实体类与表的映射关系
-->
<id property="id" column="id"/>
<result property="sname" column="sname"/>
<result property="age" column="age"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
<select id="selectReturnValue" resultMap="resultMap">
select teacher.ttid,teacher.tname,teacher.qq,student.sname from student inner join teacher on student.tid=teacher.ttid
</select>
</mapper>
接口内容
List<Teacher> selectReturnValue();