包括JDBC、JPA、多数据源和事务。
开发工具:sts
1、属性配置文件(application.properties)
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/zzch
username: root
password: password
jpa:
hibernate:
ddl-auto: update
show-sql: true
在这里值得一提的是,无论是Spring Boot默认的DataSource配置还是你自己的DataSource bean,都会引用到外部属性文件中的属性配置。所以假设你自定义的DataSource bean,你可以在定义bean时设置属性,也可以在属性文件中,以“spring.datasource.*”的方式使属性配置外部化。
2、pom.xml 配置maven依赖
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Spring Boot JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
3、Java代码范例
- package com.b505.bean;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Table;
-
- @Entity
- @Table(name="user")
- public class User {
- @Id//告诉系统id为主键
- @GeneratedValue//对主键提供生成策略
- private Integer id;
- @Column(name = "name")
- private String name;
- @Column(name = "age")
- private int age;
- @Column(name = "hobby")
- private String hobby;
- public Integer getId() {
- return id;
- }
- public void setId(Integer 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 getHobby() {
- return hobby;
- }
- public void setHobby(String hobby) {
- this.hobby = hobby;
- }
-
- }
com.b505.dao.UserDao.java
- package com.b505.dao;
- import org.springframework.data.jpa.repository.JpaRepository;
- import com.b505.bean.User;
- public interface UserDao extends JpaRepository<User, Integer> {
- /**
- * JpaRepository接口的 方法
- * delete删除或批量删除
- * findAll查询所用
- * findOne查询单个
- * save保存单个或保存批量
- * saveAndFlush保存并刷新到数据库
- * */
- }
- package com.b505.web;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import com.b505.bean.User;
- import com.b505.dao.UserDao;
- import com.fasterxml.jackson.core.sym.Name;
-
- @RestController
- public class UserController {
- @Autowired
- private UserDao userDao;
- //删除
- @RequestMapping(value ="/delete" ,method = RequestMethod.POST)
- public void weekdaylDelete(@RequestParam("id") Integer id){
- System.out.println("删除执行");
- userDao.delete(id);
- }
- //添加
- @RequestMapping(value ="/add" ,method = RequestMethod.POST)
- public void weekdayAdd(@RequestParam("name") String name,
- @RequestParam("age") Integer age,@RequestParam("hobby") String hobby
- ){
- User user = new User();
- user.setName(name);
- user.setAge(age);
- user.setHobby(hobby);
- userDao.save(user);
- }
- //查询所有
- @RequestMapping(value ="/getall" ,method = RequestMethod.GET)
- public List<User> girlList(){
- System.out.println("查询所有执行");
- return userDao.findAll();
- }
- //更改
- @RequestMapping(value="/update",method = RequestMethod.POST)
- public void weekdayUpdate(@RequestParam("id") Integer id,@RequestParam("name") String name,
- @RequestParam("age") Integer age,@RequestParam("hobby") String hobby)
- {
- User user =new User();
- System.out.println("2");
- user=userDao.findOne(id);
- if(user==null)
- System.out.println("1");
- if(name!=null)
- user.setName(name);
- //System.out.println(user.getName());
- if(age!= 0)
- user.setAge(age);
- if(hobby!=null)
- user.setHobby(hobby);
- userDao.save(user);
- System.out.println("1");
- }
- }
-