• 欢迎访问VPS岛网站,国外VPS,国内VPS,国外服务器,国内服务器,服务器主机,测评及优惠码,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站 QQ群

spring boot 使用 jdbcTemplate 连接 mysql

mysql技术 北极冷冷冷 139次浏览 已收录 0个评论

spring boot + mysql
spring boot 访问mysql主要有几种方式
方式一 :spring boot+jdbcTemplate
方式二:spring data jpa
方式三:spring boot + mybaits

一 :
spring boot+jdbcTemplate
1.引入jar包:

 <dependencies>
<!-- web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- test -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<!--jdbc -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<!-- mysql驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

</dependencies>

  1. 配置数据库参数:

    spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

    spring.datasource.username=root

    spring.datasource.password=root

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.编写entity dao service controller:

public class User {

private Long id;

private String name;

private int age;

private String sex;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

}

@Repository
public class UserDao {

@Autowired
JdbcTemplate jdbcTemplate;

public  User getUserById(long id){
    List<User> list = jdbcTemplate.query("select * from user where id = ?", new Object[]{id}, new BeanPropertyRowMapper(User.class));
    if(list!=null && list.size()>0){
        User account = list.get(0);
        return account;
    }else{
        return null;
    }
}

public  int save(User user){
    return jdbcTemplate.update("insert  into user (id,name,age,sex) values (?,?,?,?)", user.getId(),user.getName(),user.getAge(),user.getSex());

}

public  int update(User user){
    return jdbcTemplate.update("update user set name = ? ,age = ? ,sex = ? where id = ?", user.getName(),user.getAge(),user.getSex(),user.getId());

}

}

@Service
public class UserService {

@Autowired
UserDao userDao;

public int add(User user){
  return   userDao.save(user);
}

public int update(User user){
    return   userDao.update(user);
}

public User getById(long id){
    return   userDao.getUserById(id);
}

}

@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
UserService userService;

@RequestMapping(value = "/add",method = RequestMethod.POST)
public int addUser(@RequestBody User user){

    return userService.add(user);
}

@RequestMapping(value = "/update",method = RequestMethod.PUT)
public int updateUser(@RequestBody User user){

    return userService.update(user);
}

@RequestMapping(value = "/find",method = RequestMethod.GET)
public User updateUser(long id){
    return userService.getById(id);
}

}

最后启动项目


VPS岛 的文章和资源来自互联网,仅作为参考资料,如果有侵犯版权的资源请尽快联系站长,我们会在24h内删除有争议的资源。丨 转载请注明spring boot 使用 jdbcTemplate 连接 mysql
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址