一、实现功能
通过Mybatis实现快速访问后端pgsql、mysql等数据库。
二、具体步骤
1.修改pom.xml,添加mybatis相关依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
2.修改application.yml,修改数据库连接信息
spring:
application:
name: spring-boot-tutorial
datasource:
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://127.0.0.1:5432/tvseries?stringtype=unspecified&ApplicationName=tutorial
username: postgres
password: 123456
jackson:
serialization:
write-dates-as-timestamps: true #使用数值timestamp表示日期
mybatis.configuration.mapUnderscoreToCamelCase: true
备注:
(1)配置url,username和password三个参数,依据自己pgsql的环境
(2)mybatis.configuration.mapUnderscoreToCamelCase:是否采用驼峰命名法的转换
3.修改启动类,增加@MapperScan("cn.devmgr.tutorial.dao")注解
备注:@MapperScan("cn.devmgr.tutorial.dao")中cn.devmgr.tutorial.dao是数据访问层的类。
4.添加Mybatis Mapping接口类
(1)接口:TvSeriesDao.java
package cn.devmgr.tutorial.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.devmgr.tutorial.pojo.TvSeries;
public interface TvSeriesDao {
@Select("select * from tv_series where id=#{id}")
public TvSeries getOneById(int id);
@Select("select * from tv_series where status=0")
public List<TvSeries> getAll();
public int update(TvSeries tvSeries);
public int insert(TvSeries tvSeries);
@Delete("delete from tv_series where id=#{id}")
public int delete(int id);
@Update("update tv_series set status=-1, reason=#{reason} where id=#{id}")
public int logicDelete(int id, String reason);
}
(2)接口:TvCharacterDao.java
package cn.devmgr.tutorial.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import cn.devmgr.tutorial.pojo.TvCharacter;
@Repository
public interface TvCharacterDao {
@Select("select * from tv_character where id=#{id}")
public TvCharacter getOneById(int id);
@Select("select * from tv_character where tv_series_id=#{tvSeriesId}")
public List<TvCharacter> getAllByTvSeriesId(int tvSeriesId);
public int update(TvCharacter tvCharacter);
public int insert(TvCharacter tvCharacter);
@Delete("delete from tv_character where id=#{id}")
public int delete(int id);
}
5.添加Mapping对应的XML
针对复杂的sql,不方便直接在接口类实现的,通常可以在xml中实现
(1)TvSeriesDao.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="cn.devmgr.tutorial.dao.TvSeriesDao" >
<insert id="insert" parameterType="cn.devmgr.tutorial.pojo.TvSeries"
useGeneratedKeys="true" keyProperty="id">
insert into tv_series (name, season_count, origin_release )
values (#{name}, #{seasonCount}, #{originRelease} )
</insert>
<update id="update" parameterType="cn.devmgr.tutorial.pojo.TvSeries">
update tv_series set name=#{name}, season_count=#{seasonCount}, origin_release=#{originRelease} where id=#{id}
</update>
</mapper>
(2)TvCharacterDao.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="cn.devmgr.tutorial.dao.TvCharacterDao" >
<insert id="insert" parameterType="cn.devmgr.tutorial.pojo.TvCharacter"
useGeneratedKeys="true" keyProperty="id">
insert into tv_character (name, tv_series_id, photo )
values (#{name}, #{tvSeriesId}, #{photo} )
</insert>
<update id="update" parameterType="cn.devmgr.tutorial.pojo.TvCharacter">
update tv_character set name=#{name}, tv_series_id=#{tvseriesid}, photo=#{photo} where id=#{id}
</update>
</mapper>
备注:
(1)<mapper namespace="cn.devmgr.tutorial.dao.TvSeriesDao" >:namespace是唯一标识,指向对应的Dao接口类
(2)<insert id="insert" parameterType="cn.devmgr.tutorial.pojo.TvSeries" useGeneratedKeys="true" keyProperty="id"> ,其中,id需要和程序中insert方法名称一致,parameterType是参数类。
(3)xml所在包名,一定要和其对应的Dao层一致(如下图1和2要一样的包名,否则会报错)
三、参考
1.源码参考:
https://github.com/gexiangdong/tutorial/tree/master/section-02
2.博客
https://blog.csdn.net/sundacheng1989/article/details/81630370