Springboot与JdbcTemplate
小案例
目录结构
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
数据库
entity
package Springboot.entity; public class student { private Integer stuid; private String stuname; private Integer age; public Integer getStuid() { return stuid; } public void setStuid(Integer stuid) { this.stuid = stuid; } public String getStuname() { return stuname; } public void setStuname(String stuname) { this.stuname = stuname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public student() { } public student(Integer stuid, String stuname, Integer age) { this.stuid = stuid; this.stuname = stuname; this.age = age; } }
dao
public interface Istudao { public int insertstu(student stu); public int updatestu(student stu); public int deletestu(Integer id); public List<student> findAll(); }
daoimpl
@Repository public class Istudaoimpl implements Istudao { @Resource private JdbcTemplate jdbcTemplate; @Override public int insertstu(student stu) { return jdbcTemplate.update("insert into student (stuid,stuname,age) values(?,?,?)",stu.getStuid(),stu.getStuname(),stu.getAge()); } @Override public int updatestu(student stu) { return jdbcTemplate.update("update student set stuname=? where stuid=?",stu.getStuname(),stu.getStuid()); } @Override public int deletestu(Integer id) { return jdbcTemplate.update("delete from student where stuid=?",id); } @Override public List<student> findAll() { //封装行数据映射 RowMapper<student> rowMapper=new RowMapper<student>() { @Override public student mapRow(ResultSet rs, int rowNum) throws SQLException { student stu=new student(rs.getInt("stuid"),rs.getString("stuname"),rs.getInt("age")); return stu; } }; return jdbcTemplate.query("select * from student", rowMapper); } }
service
public interface Istuservice { public int insertstu(student stu); public int updatestu(student stu); public int deletestu(Integer id); public List<student> findAll(); }
serviceimpl
@Service("istuservice") public class Istuserviceimpl implements Istuservice { @Resource private Istudao istudao; @Override public int insertstu(student stu) { return istudao.insertstu(stu); } @Override public int updatestu(student stu) { return istudao.updatestu(stu); } @Override public int deletestu(Integer id) { return istudao.deletestu(id); } @Override public List<student> findAll() { return istudao.findAll(); } }
Controller
@RestController @RequestMapping("/stu") public class stuController { @Resource private Istuservice istuservice; @RequestMapping("/insertstu") public int insertstu(){ System.out.println("insert"); return istuservice.insertstu(new student(10,"S1",23)); } @RequestMapping("/updatestu") public int updatestu(){ System.out.println("update"); return istuservice.updatestu(new student(10,"S2",10012)); } @RequestMapping("/deletestu") public int deletestu(){ System.out.println("delete"); return istuservice.deletestu(10); } @RequestMapping("/findAll") public List<student> findAll(){ System.out.println("select"); return istuservice.findAll(); } }
application
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///student username: root password: 123 ##更改Tomcat端口 server: port: 8081 ##指定当前工程项目访问地址 context-path: /jdbc
主程序
@SpringBootApplication public class Istu { public static void main(String[] args) { SpringApplication.run(Istu.class,args); } }
启动
其他省略