一、引入依赖:
SpringBoot版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
在pom文件引入spring-boot-starter-jdbc的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
mysql连接:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
配置相关文件
在application.properties文件配置mysql的驱动类,数据库地址,数据库账号、密码信息。
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=******
二、具体编码
实体类(Entity)
package com.yms.demo.domain.pojo;
import java.io.Serializable;
import java.util.Date;
public class CeNote implements Serializable{
private Integer id;
private String noterId;
private Integer courseId;
private Integer videoId;
private String content;
private Integer favourNum;
private String time;
get...set....
}
dao层
interface:
package com.yms.demo.dao;
import com.yms.demo.domain.pojo.CeNote;
import java.util.List;
public interface CeNoteRepo {
public List<CeNote> findAll();
public CeNote findOne(Integer id);
public int add(CeNote note);
public int update(CeNote note);
public int delete(Integer id);
}
Impl:
package com.yms.demo.dao.impl;
import com.yms.demo.dao.CeNoteRepo;
import com.yms.demo.domain.pojo.CeNote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class CeNoteRepoImpl implements CeNoteRepo {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<CeNote> findAll() {
List<CeNote> res = jdbcTemplate.query("select * from ce_note", new Object[]{},
new BeanPropertyRowMapper(CeNote.class));
return res;
}
@Override
public CeNote findOne(Integer id) {
List<CeNote> res = jdbcTemplate.query("select * from ce_note where id = ?",new Object[]{id},
new BeanPropertyRowMapper(CeNote.class));
if(res!=null && res.size()>0){
return res.get(0);
}else{
return null;
}
}
@Override
public int add(CeNote note) {
return jdbcTemplate.update("INSERT INTO ce_note(noter_id,course_id,video_id,content,favour_num,time)" +
" VALUES (?,?,?,?,?,?)",
note.getNoterId(),note.getCourseId(),note.getVideoId(),note.getContent(),note.getFavourNum(),
note.getTime());
}
@Override
public int update(CeNote note) {
return jdbcTemplate.update("UPDATE ce_note set noter_id=?,course_id=?,video_id=?,content=?,favour_num=?,time=? where id=?",
note.getNoterId(),note.getCourseId(),note.getVideoId(),note.getContent(),note.getFavourNum(),
note.getTime(),note.getId());
}
@Override
public int delete(Integer id) {
return jdbcTemplate.update("DELETE FROM TABLE ce_note WHERE id=?",id);
}
}
service层
interface:
package com.yms.demo.service;
import com.yms.demo.domain.pojo.CeNote;
import java.util.List;
public interface CeNoteService {
public List<CeNote> findAll();
public CeNote findOne(Integer id);
public int add(CeNote note);
public int update(CeNote note);
public int delete(Integer id);
}
Impl:
package com.yms.demo.service.impl;
import com.yms.demo.common.utils.DateHelpler;
import com.yms.demo.dao.CeNoteRepo;
import com.yms.demo.domain.pojo.CeNote;
import com.yms.demo.service.CeNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CeNoteServiceImpl implements CeNoteService {
@Autowired
private CeNoteRepo ceNote;
@Override
public List<CeNote> findAll() {
return ceNote.findAll();
}
@Override
public CeNote findOne(Integer id) {
return ceNote.findOne(id);
}
@Override
public int add(CeNote note) {
note.setTime(DateHelpler.getDateNow());
return ceNote.add(note);
}
@Override
public int update(CeNote note) {
note.setTime(DateHelpler.getDateNow());
return ceNote.update(note);
}
@Override
public int delete(Integer id) {
return ceNote.delete(id);
}
}
WebController层
package com.yms.demo.web;
import com.yms.demo.domain.entity.Result;
import com.yms.demo.domain.pojo.CeNote;
import com.yms.demo.service.CeNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@CrossOrigin//跨域所用
@RestController
public class CeNoteController {
@Autowired
private CeNoteService ceNoteService;
@RequestMapping("/findAll")
public List<CeNote> findAll() {
return ceNoteService.findAll();
}
@RequestMapping("/findOne")
public CeNote findOne(Integer id) {
return ceNoteService.findOne(id);
}
@PostMapping("/add")
public Result add(@RequestBody CeNote note) {
try {
ceNoteService.add(note);
return new Result(true,"成功");
} catch (Exception e) {
return new Result(false,"失败");
}
}
}