您的位置:首页 > 编程语言 > Java开发

springboot mybatis整合

2018-08-25 14:36 519 查看

1:build.gradle 添加依赖包

compile group: 'org.postgresql', name: 'postgresql', version: '42.2.4'
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.3.0'

2:测试代码

(1)FwjlVo.java

@Data
public class FwjlVo {
private Integer id;
private String yhid;
private String gnid;
private String czsj;
private String ip;
private String content;
}

(2)FwjlDao.java

@Mapper
public interface FwjlDao {
int add(FwjlVo fwjlVo);

FwjlVo getFwjlById(int id);

List<FwjlVo> getFwjlList();
}

(3)FwjlService.java

@Service
public interface FwjlService {
boolean add(FwjlVo fwjlVo);

FwjlVo getFwjlById(int id);

List<FwjlVo> getFwjlList();
}

(4)FwjlServiceImpl.java

@Component
public class FwjlServiceImpl implements FwjlService{
@Resource
private FwjlDao fwjlDao;

@Override
public boolean add(FwjlVo fwjlVo) {
return fwjlDao.add(fwjlVo) > 0;
}

@Override
public FwjlVo getFwjlById(int id) {
return fwjlDao.getFwjlById(id);
}

@Override
public List<FwjlVo> getFwjlList() {
return fwjlDao.getFwjlList();
}
}

3:配置文件

(1)application.properties

#===============PostgreSQL config=====================
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driverClassName=org.postgresql.Driver

#===============Mybatis config=====================
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.example.demo.xtxx

(2)FwjlMapper.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="com.example.demo.xtxx.FwjlDao">
<resultMap id="BaseResultMap" type="com.example.demo.xtxx.FwjlVo" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="yhid" property="yhid" jdbcType="VARCHAR" />
<result column="gnid" property="gnid" jdbcType="VARCHAR" />
<result column="czsj" property="czsj" jdbcType="VARCHAR" />
<result column="ip" property="ip" jdbcType="VARCHAR" />
<result column="content" property="content" jdbcType="VARCHAR" />
</resultMap>

<insert id="add" parameterType="com.example.demo.xtxx.FwjlVo">
INSERT  INTO xtxx_fwjl(yhid,gnid,ip,content)
VALUES (#{yhid,jdbcType=VARCHAR},#{gnid,jdbcType=VARCHAR},#{ip,jdbcType=VARCHAR},#{content,jdbcType=VARCHAR})
</insert>

<select id="getFwjlById" parameterType="java.lang.Integer"  resultMap="BaseResultMap">
SELECT id,yhid,gnid,to_char(czsj, 'YYYY-MM-DD HH24:MI:SS') czsj,ip,content FROM xtxx_fwjl WHERE id=#{id,jdbcType=INTEGER}
</select>

<select id="getFwjlList" resultMap="BaseResultMap">
SELECT id,yhid,gnid,to_char(czsj, 'YYYY-MM-DD HH24:MI:SS') czsj,ip,content FROM xtxx_fwjl
</select>
</mapper>

 

4:测试

  @Autowired
FwjlService fwjlService;

@RequestMapping(
value = "addFwjl",
method = RequestMethod.POST
)
public String addFwjl(@RequestBody FwjlVo vo){
boolean flag = fwjlService.add(vo);

int no = 0;
String msg = "保存失败";

if(flag){
no = 1;
msg = "保存成功";
}

Map<String,Object> map = new HashMap<String,Object>();
map.put("no", no);
map.put("msg", msg);

Gson gson = new Gson();

return gson.toJson(map);
}

@RequestMapping(
value = "getFwjl",
method = RequestMethod.GET
)
public String getFwjl(@RequestParam(required = true) int id){
FwjlVo fwjlVo = fwjlService.getFwjlById(id);
Gson gson = new Gson();

return gson.toJson(fwjlVo);
}

@RequestMapping(
value = "getFwjlList",
method = RequestMethod.GET
)
public String getFwjlList(){
List<FwjlVo> list = fwjlService.getFwjlList();
Gson gson = new Gson();

return gson.toJson(list);
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: