您好,登录后才能下订单哦!
下文内容主要给大家带来spring boot 访问mysql的主要方式有哪些,这里所讲到的知识,与书籍略有不同,都是亿速云专业技术人员在与用户接触过程中,总结出来的,具有一定的经验分享价值,希望给广大读者带来帮助。
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>
配置数据库参数:
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);
}
}
最后启动项目
对于以上关于spring boot 访问mysql的主要方式有哪些,如果大家还有更多需要了解的可以持续关注我们亿速云的行业推新,如需获取专业解答,可在官网联系售前售后的,希望该文章可给大家带来一定的知识更新。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。