接口
package com.q.dao;
import com.q.pojo.Student1;
import java.util.List;
public interface StudentMapper1 {
Student1 getStudent(int age);
List<Student1> getInformation(Student1 student1);
int updateInformation(Student1 student1);
int insertData(Student1 student1);
List<Student1> bulkQuery(List<Integer> list);
int batchAdd(List<Student1> list);
int batchDeletion(List<Integer> list);
}
映射实现配置
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.q.dao.StudentMapper1">
<insert id="insertData" parameterType="Student1">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id !=null and id !=''">
id,
if>
<if test="name !=null and name !=''">
name,
if>
<if test="tid !=null and tid !=''">
tid,
if>
<if test="age !=null and age !=''">
age,
if>
trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id !=null and id !=''">
#{id},
if>
<if test="name !=null and name !=''">
#{name},
if>
<if test="tid !=null and tid !=''">
#{tid},
if>
<if test="age !=null and age !=''">
#{age},
if>
trim>
insert>
<insert id="batchAdd" parameterType="Student1">
insert into student(
id, name, tid, age
)values
<foreach collection="list" index="index" item="Student1" separator=",">
(#{Student1.id},#{Student1.name},#{Student1.tid},#{Student1.age})
foreach>
insert>
<update id="updateInformation" parameterType="Student1">
update student
<set>
<if test="name !=null and name !=''">
name =#{name},
if>
<if test="age !=null and age !=''">
age=#{age},
if>
<if test="tid !=null and tid !=''">
tid=#{tid},
if>
set>
where id=#{id};
update>
<delete id="batchDeletion" parameterType="list">
delete from student where id in
<foreach collection="list" open="(" close=")" separator="," item="id">
#{id}
foreach>
delete>
<select id="getStudent" resultType="com.q.pojo.Student1">
select * from mybatis.student
<where>
<if test="age != null">
age=#{age}
if>
where>
select>
<select id="getInformation" resultType="com.q.pojo.Student1" parameterType="Student1">
select *
from student
<where>
<bind name="name1" value="'%'+name+'%'"/>
<if test="name !=null and name !=''">
and name like #{name1}
if>
<if test="age !=null and age !=''">
and age=#{age}
if>
<if test="id !=null and id !=''">
and id=#{id}
if>
where>
select>
<select id="bulkQuery" resultType="com.q.pojo.Student1" parameterType="list">
select *
from student where id in
<foreach collection="list" open="(" close=")" separator="," item="id" index="i">
#{id}
foreach>
select>
mapper>
接口2
package com.q.dao;
import com.q.pojo.Blog;
import java.util.List;
import java.util.Map;
public interface BlogMapper {
//插入数据
int addBook(Blog blog);
//查询博客
List<Blog> QueryBlog(Map<String, Object> map);
List<Blog> QueryBlogChoose(Map<Object, Object> map);
int upDateTest(Map<Object, Object> map);
// 查询id 1 2 3的博客
List<Blog> selectBlog(Map<Object, Object> map);
List<Blog> selectBlogByName(String string);
}
映射实现配置
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.q.dao.BlogMapper">
<insert id="addBook" parameterType="blog">
insert into mybatis.blog(id, title, author, create_time, views)
values (#{id}, #{title}, #{author}, #{create_time}, #{views});
insert>
<update id="upDateTest">
update mybatis.blog
<set>
<if test="title !=null">
title =#{title},
if>
set>
where id =#{id}
update>
<select id="QueryBlog" parameterType="map" resultType="com.q.pojo.Blog">
select *
from mybatis.blog where 1=1
<if test="title !=null">
and title=#{title}
if>
<if test="author !=null">
and author=#{author}
if>
select>
<sql id="choose-when">
<choose>
<when test="title!=null">
title=#{title}
when>
<when test="author!=null">
and author=#{author}
when>
<otherwise>
and views=#{views}
otherwise>
choose>
sql>
<select id="QueryBlogChoose" resultType="com.q.pojo.Blog" parameterType="map">
select * from mybatis.blog
<where>
<include refid="choose-when"/>
where>
select>
<select id="selectBlog" resultType="com.q.pojo.Blog" parameterType="map">
select *
from mybatis.blog
<where>
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id =#{id}
foreach>
where>
select>
<select id="selectBlogByName" resultType="com.q.pojo.Blog">
<bind name="blogName" value="'%'+title+'%'"/>
select * from mybatis.blog where title like #{blogName};
select>
mapper>