项目结构
建立表格,实体类
model包下创建Book.java
dao层:定义的方法名和xml映射文件中的id一致,mybatis会自动对应上
@Mapper注解
package tutorial.springboot.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import tutorial.springboot.model.Book;
@Mapper
public interface BookDao {
void save(Book book);
void delete(Integer id);
void modify(Book book);
Book queryById(@Param(value="id")Integer id);
List<Book> query();
//批量插入
void saveBatch(List<Book> bookList);
}
映射文件
<?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="tutorial.springboot.dao.BookDao" >
<!-- type为实体类Book,包名已经配置,可以直接写类名 -->
<resultMap type="Book" id="BaseMap">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="price" property="price" jdbcType="FLOAT" />
<result column="produce_time" property="produceTime" jdbcType="VARCHAR" />
</resultMap>
<insert id="save" parameterType="tutorial.springboot.model.Book" useGeneratedKeys="true" keyProperty="id" >
insert into book
<trim prefix="(" suffix=")" suffixOverrides=",">
id,name,price,produce_time,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{price,jdbcType=FLOAT},
#{produceTime,jdbcType=VARCHAR},
</trim>
</insert>
<insert id="saveBatch" parameterType="java.util.List" useGeneratedKeys="true">
<selectKey resultType="int" keyProperty="id" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into book
<trim prefix="(" suffix=")" suffixOverrides=",">
id,name,price,produce_time,
</trim>
<trim prefix="values">
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id, jdbcType=INTEGER},
#{item.name, jdbcType=VARCHAR},
#{item.price, jdbcType=FLOAT},
#{item.produceTime, jdbcType=VARCHAR})
</foreach>
</trim>
</insert>
<delete id="delete">
<![CDATA[
delete from book where id = #{id,jdbcType=INTEGER}
]]>
</delete>
<select id="query" resultMap="BaseMap">
select * from book
</select>
<select id="queryById" resultMap="BaseMap">
select * from book where 1=1
<if test="id != 0">
and id = #{id};
</if>
</select>
<update id="modify" parameterType="tutorial.springboot.model.Book">
update book
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
Service层
@Service
public class BookService{
@Resource
private BookDao bookDao;
public void save(Book book) {
bookDao.save(book);
}
public void saveBatch(List<Book> bookList){
bookDao.saveBatch(bookList);
}
public void delete(Integer id){
bookDao.delete(id);
}
public void modify(Book book){
bookDao.modify(book);
}
public Book queryById(Integer id){
return bookDao.queryById(id);
}
public List<Book> query(){
return bookDao.query();
}
}
测试
//SpringJUnit支持,由此引入Spring-Test框架支持
@RunWith(SpringJUnit4ClassRunner.class)
//指定我们SpringBoot工程的Application启动类
@SpringApplicationConfiguration(classes = App.class)
public class BookXMLTest {
@Resource
private BookService bookService;
//新增
@Test
public void saveBook(){
Book book = new Book();
book.setName("hibernate");
book.setPrice(102.1f);
book.setProduceTime(new Date());
bookService.save(book);
}
@Test
public void saveBatchBook(){
Book book1 = new Book();
book1.setName("hibernate6");
book1.setPrice(102.1f);
book1.setProduceTime(new Date());
Book book2 = new Book();
book2.setName("hibernate7");
book2.setPrice(102.1f);
book2.setProduceTime(new Date());
List<Book> bookList = new ArrayList<Book>();
bookList.add(book1);
bookList.add(book2);
bookService.saveBatch(bookList);
}
//删除
@Test
public void deleteBook(){
bookService.delete(10);
}
//更新
@Test
public void modifyBook(){
Book book = bookService.queryById(3);
System.out.println("book==="+book);
book.setName("test");
bookService.modify(book);
}
//查询
@Test
public void queryBook(){
List<Book> bookList = bookService.query();
System.out.println(bookList);
}
}