1:设计好数据表。
2:写Mybaits的配置文件
3:编写与数据表对应的持久化类。
4:编写mapper。如果采用XML的方式,需要为持久化类编写mapper配置文件。如果采用注解方式,为持久化类编写相应的Mapper类。
5:写一个MyBatis的工具类,用来创建SqlSession
6:最好创建service类,为每个持久化类进行操作的封装。
7:然后就调用封装好的service进行操作。
config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--MyBatis的配置文件。-->
<configuration>
<!--引用Java属性文件-->
<properties resource="jdbc.properties"></properties>
<!--配置数据源。可配置多个。default指定使用的数据源id-->
<environments default="development">
<!--配置单独的数据源-->
<environment id="development">
<!--配置事务管理,默认采用JDBC事务管理-->
<transactionManager type="JDBC"></transactionManager>
<!--指定具体的数据源配置。type属性指定是否使用连接池-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--指定mapper包名-->
<package name="com.imooc.cake.mapper"></package>
</mappers>
</configuration>
Mybatis工具类:MyBatisUtils
package com.imooc.cake.common;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
/**
* MyBatis的工具类
*/
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static{
String resource = "config.xml";
try {
//创建配置文件的输入流
reader = Resources.getResourceAsReader(resource);
//创建SqlSessionFactory,也就是数据库会话的工厂类
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
//创建会话
public static SqlSession openSession(){
return sqlSessionFactory.openSession();
}
}
持久化类:Cake
package com.imooc.cake.entity;
import java.util.Date;
public class Cake {
private Long id;
private Long categoryId;
private String name;
private Integer level;
private Integer price;
private byte[] smallImg;
private Date createTime;
private Date updateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getCategoryId() {
return categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public byte[] getSmallImg() {
return smallImg;
}
public void setSmallImg(byte[] smallImg) {
this.smallImg = smallImg;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
持久化类对应的Mapper类:CakeMapper
package com.imooc.cake.mapper;
import com.imooc.cake.entity.Cake;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface CakeMapper {
@Select("select * from cake order by create_time desc limit #{sikp},#{size}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column = "category_id",property="categoryId"),
@Result(column = "name",property="name"),
@Result(column = "level",property="level"),
@Result(column = "price",property="price"),
@Result(column = "create_time",property="createTime"),
@Result(column = "update_time",property="updateTime"),
})
List<Cake> getCakes(Integer skip, Integer size);
@Select("select id, category_id,name,level,price,create_time createTime,update_time updateTime from" +
" cake where category_id=#{categoryId} order by create_time desc limit #{sikp},#{size}")
List<Cake> getCakesByCategoryId(@Param("categoryId")Long categoryId, @Param("skip")Integer skip, @Param("size")Integer size);
@Select("select count(*) from cake where category_id = #{categoryId}")
int countCakesByCategoryId(@Param("categoryId")Long categoryId);
@Insert("insert into cake(category_id,name,level,price,small_img,create_time,update_time) " +
"value(#{cake.categoryId},#{cake.name},#{cake.level},#{cake.price},#{cake.smallImg},#{cake.createTime},#{cake.updateTime})")
void addCake(Cake cake);
@Select("select small_img from cake where id = #{id} for update")
Cake getImg(@Param("id")Long id);
}
service类:CakeService
package com.imooc.cake.service;
import com.imooc.cake.common.MyBatisUtils;
import com.imooc.cake.entity.Cake;
import com.imooc.cake.mapper.CakeMapper;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class CakeService {
public List<Cake> getCakesByCategoryId(Long categoryId, Integer page, Integer size){
SqlSession sqlSession = MyBatisUtils.openSession();
try {
CakeMapper mapper = sqlSession.getMapper(CakeMapper.class);
return mapper.getCakesByCategoryId(categoryId, (page - 1) * size, size);
}finally{
sqlSession.close();
}
}
}