SpringBoot之连接mysql数据库

上一篇:Spring Boot 之网页搭建

0.pom文件

需要增加两个依赖:

       

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        

        
            mysql
            mysql-connector-java
        

1.创建测试数据库

mysql> create database db_example; -- Create the new database
mysql> create user 'springuser'@'%' identified by 'ThePassword'; -- Creates the user
mysql> grant all on db_example.* to 'springuser'@'%'; -- Gives all the privileges to the new user on the newly created database

2.给application.properties添加配置

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword

3.增加一个User.scala的Entity文件

SpringBoot之连接mysql数据库_第1张图片
User.scala
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@Entity // This tells Hibernate to make a table out of this class
class User {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private var id : Integer= 0

  private var name:String = null

  private var email:String  = null

  def getId: Integer = id

  def setId(id: Integer): Unit = {
    this.id = id
  }

  def getName: String = name

  def setName(name: String): Unit = {
    this.name = name
  }

  def getEmail: String = email

  def setEmail(email: String): Unit = {
    this.email = email
  }
}

4.建立一个UserRepository.scala接口文件

SpringBoot之连接mysql数据库_第2张图片
UserRepository.scala
import org.springframework.data.repository.CrudRepository

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
trait UserRepository extends CrudRepository[User, Integer]{

}

5.在ViewingController.scala里添加代码

ViewingController.scala在上一篇Spring Boot 之网页搭建里说到,是一个注解为@Controller 的controller层

SpringBoot之连接mysql数据库_第3张图片
ViewingConrtoller

@Autowired // This means to get the bean called userRepository private // Which is auto-generated by Spring, we will use it to handle the data
  var userRepository: UserRepository = null

@GetMapping(path = Array("/add")) // Map ONLY GET Requests
  @ResponseBody
  def addNewUser(@RequestParam  name: String, @RequestParam  email: String): String =  { // @ResponseBody means the returned String is the response, not a view name
  // @RequestParam means it is a parameter from the GET or POST request
    val n = new User
    n.setName(name)
    n.setEmail(email)
    userRepository.save(n)
    "Saved"
  }

  @GetMapping (path = Array ("/all") )
  @ResponseBody
  def getAllUsers: java.lang.Iterable[User] = {
    // This returns a JSON or XML with the users
     userRepository.findAll
  }

@ResponseBody:表示返回的是数据,而不是页面

5.启动

1)在IDEA里,可以直接选择DemoApplication里的main方法,直接启动。
2)或者打成jar包,用命令的方式启动
jar -jar  demo.jar

6.测试

1)通过http://localhost:8080/add?name=First&email=someemail@someemailprovider,访问接口。
返回结果为:

Saved

2)通过带参数http://localhost:8080/all访问
返回结果为:

[{"id":1,"name":"First","email":"[email protected]"}]
文章参考:https://spring.io/guides/gs/accessing-data-mysql/

你可能感兴趣的:(SpringBoot之连接mysql数据库)