spring boot mysql数据库配置并对数据库进行增删改查操作,spring boot可以不需要任何配置就能云心,使用也是挺广泛的,由于网上的spring boot教程都是你抄我,我抄你,看着都烦,因此自己做了一个spring boot教程,仅作为参考,复制即可用。
第一步:创建项目,引入spring boo的jar包,本案例采用maven引入,在pom.xml中加入如下代码。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <groupId>com.baidu.springBootDemo</groupId> <artifactId>springBootDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <main.basedir>${basedir}/../..</main.basedir> </properties> <dependencies> <!-- Compile --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Runtime --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!-- Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第二步:在src目录下增加application.properties mysql数据库配置文件,并加入数据库连接信息。
#DB Configuration: spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true spring.datasource.username = root spring.datasource.password = root spring.h2.console.enabled=true logging.level.org.hibernate.SQL=debug #JPA Configuration: spring.jpa.database=MySQL spring.jpa.show-sql=true spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
第三步:根据数据库表创建实体类,本人创建了Student学生类,代码如下。
package com.baidu.domain import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="student") public class Student implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; private int age; private int score; private String address; public int getId() { return id; } public Student(int id, String name, int age, int score, String address) { super(); this.id = id; this.name = name; this.age = age; this.score = score; this.address = address; } public Student() { super(); } public void setId(int 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 int getScore() { return score; } public void setScore(int score) { this.score = score; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public static long getSerialversionuid() { return serialVersionUID; } }
第四步:创建接口类,继承JpaRepository<T,ID>接口,参数1:实体类;参数2:id的类型,注意,JpaRepository内部已经实现了crud增删改查接口,不需要我们写任何方法或接口了,方法名相同,系统会优先选择你定义的接口方法,如下。
package com.baidu.service; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import com.baidu.domain.Student; public interface StudentRepository extends JpaRepository<Student, Integer>{ //系统会自动调用同名的方法,也可以不定义该方法 public List<Student>findAll(); }
第五步:写controller类进行测试我们的spring boot项目,代码如下。
package com.baidu.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.baidu.domain.Student; import com.baidu.service.StudentRepository; @RequestMapping("/home") @Controller public class StudentController { @Autowired private StudentRepository repos; //查询所有 http://localhost:8080/home/getAll @RequestMapping("/getAll") @ResponseBody public List<Student>findAll(){ return repos.findAll(); } //根据id查询 http://localhost:8080/home/findById?id=1 @RequestMapping("/findById") @ResponseBody public Student findById(int id){ return repos.findOne(id); } //插入数据 http://localhost:8080/home/insert @RequestMapping("/insert") @ResponseBody public String addStudent(){ Student stu = new Student(); stu.setName("李四"); stu.setAge(18); stu.setScore(90); stu.setAddress("北京"); repos.save(stu); return "插入成功!"; } //更新数据 http://localhost:8080/home/update @RequestMapping("/update") @ResponseBody public String updateStudent(){ Student stu = new Student(); //id存在则自动替你修改而不会插入数据 stu.setId(3); stu.setName("王五"); stu.setAge(12); stu.setScore(100); stu.setAddress("四川"); repos.save(stu); return "修改成功!"; } //根据id删除 http://localhost:8080/home/delete/1 @RequestMapping("/delete/{id}") @ResponseBody public String deleteStudent(@PathVariable int id){ repos.delete(id); return "删除成功!"; } }
第六步:使用@SpringBootApplication启动我们的项目,运行main方法就可以了,启动完之后我们就可以通过上面的网址访问我们的数据库了,代码。
package com.baidu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplicationRun { public static void main(String[] args) { SpringApplication.run(MyApplicationRun.class, args); } }