Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。 JdbcTemplate 是在JDBC API基础上提供了更抽象的封装,并提供了基于方法注解的事务管理能力。 通过使用SpringBoot自动配置功能并代替我们自动配置beans.

导入依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

</dependencies>

创建entity层

package com.wn.jdbc.entity;

public class Petbreed {

private Integer perId;
private String perBreed;

public Integer getPerId() {
return perId;
}

public void setPerId(Integer perId) {
this.perId = perId;
}

public String getPerBreed() {
return perBreed;
}

public void setPerBreed(String perBreed) {
this.perBreed = perBreed;
}

public Petbreed(Integer perId, String perBreed) {
this.perId = perId;
this.perBreed = perBreed;
}

public Petbreed(String perBreed) {
this.perBreed = perBreed;
}

public Petbreed() {
}
}

编写application.properties文件

#配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///petmanage
spring.datasource.username=root
spring.datasource.password=123

#配置端口号 server.port
=8003

创建dao接口层

public interface PetbreedDao {

    //新增
    public Integer getinster(Petbreed petbreed);

    //修改
    public Integer getupdate(Petbreed petbreed);

    //删除
    public Integer getdel(Integer id);

    //查询
    public List<Petbreed> getAll();

}

创建dao接口实现层

@Repository
public class PetbreedDaoImpl implements PetbreedDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer getinster(Petbreed petbreed) {
        return jdbcTemplate.update("INSERT INTO petbreed(petBreed) VALUE(?)",petbreed.getPerBreed());
    }

    @Override
    public Integer getupdate(Petbreed petbreed) {
        return jdbcTemplate.update("UPDATE petbreed SET petBreed=? WHERE petId=?",petbreed.getPerBreed(),petbreed.getPerId());
    }

    @Override
    public Integer getdel(Integer id) {
        return jdbcTemplate.update("DELETE FROM petbreed WHERE petId=?",id);
    }

    @Override
    public List<Petbreed> getAll() {
        RowMapper<Petbreed> rowMapper=new RowMapper<Petbreed>() {
            @Override
            public Petbreed mapRow(ResultSet resultSet, int i) throws SQLException {
                Petbreed petbreed=new Petbreed(resultSet.getInt("petId"),resultSet.getString("petBreed"));
                return petbreed;
            }
        };

        return jdbcTemplate.query("SELECT * FROM petbreed",rowMapper) ;
    }
}

创建service接口层

public interface PetbreedService {
    //新增
    public Integer getinster(Petbreed petbreed);

    //修改
    public Integer getupdate(Petbreed petbreed);

    //删除
    public Integer getdel(Integer id);

    //查询
    public List<Petbreed> getAll();
}

创建service接口实现层

@Service("petbreedService")
public class PetbreedServiceimpl implements PetbreedService {

    //植入dao层对象
    @Resource
    private PetbreedDao dao;

    @Override
    public Integer getinster(Petbreed petbreed) {
        return dao.getinster(petbreed);
    }

    @Override
    public Integer getupdate(Petbreed petbreed) {
        return dao.getupdate(petbreed);
    }

    @Override
    public Integer getdel(Integer id) {
        return dao.getdel(id);
    }

    @Override
    public List<Petbreed> getAll() {
        return dao.getAll();
    }
}

创建controller层

@Controller
@RequestMapping("/pet")
public class PetbreedController {

    //植入service层对象
    @Resource(name = "petbreedService")
    private PetbreedService service;

    //新增
    @RequestMapping("/getinsert")
    @ResponseBody
    public Integer getinsert(){
        Integer getinster = service.getinster(new Petbreed("呵呵"));
        return getinster;
    }

    //修改
    @RequestMapping("/getupdate")
    @ResponseBody
    public Integer getupdate(){
        Integer getupdate = service.getupdate(new Petbreed(7, "小猫"));
        return getupdate;
    }

    //删除
    @RequestMapping("/getdel")
    @ResponseBody
    public Integer getdel(){
        Integer getdel = service.getdel(7);
        return getdel;
    }

    //查询
    @RequestMapping("/getAll")
    @ResponseBody
    public List<Petbreed> getAll(){
        List<Petbreed> all = service.getAll();
        return all;
    }

}

结果实现

  新增:

      

      

  修改:

      

      

  删除:

      

 

 

       

  查询:

      

版权声明:本文为wnwn原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wnwn/p/12038220.html