您好,登錄后才能下訂單哦!
下文內容主要給大家帶來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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。