您的位置:首页 > 编程语言 > Java开发

Spring boot结合Mysql

2017-12-01 14:41 501 查看
刚接触SpringBoot之后才见识到他的强大

在搭建最基础的spring boot项目之后 结合数据库进行操作

spring boot 给我们提供了Spring data Jpa 是一个十分便利的功能

真的想感叹一下框架给我们开发带来的便利 下面是项目结构



首先在pom.xml中加入spring boot 数据库操作依赖

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>


再加入spring boot test的依赖

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>


最终的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.l</groupId>
<artifactId>springBoot</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springBoot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>

<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
</dependencies>

<!-- Package as an executable jar -->
<build>
<finalName>springBoot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


新建application.java

package springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}


在resource目录中新建application.properties配置文件来配置数据库属性,这里使用的是mysql数据库

在一开始时我使用database.properties来命名导致配置不加载 不知道是不是一定要命名成这个

spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true


新建User实体类

package springboot.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;

@Entity
public class User implements Serializable {
private static final Long serialVersionUID = 1L;
private Long id;
private String userName;
private String password;

public User() {
}

public User(Long id, String userName, String password) {
this.id = id;
this.userName = userName;
this.password = password;
}

@Id
@GeneratedValue
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Column
public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

@Column
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}


接口只需要实现JpaRepository就可以使用,实现接口之后,他的便利之处就显示出来了

接口会根据方法的名字进行sql语句的拼写,

package springboot.domain;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UserDao extends JpaRepository<User, Long> {
User findByUserName(String name);

User findByUserNameOrPassword(String username, String password);

List<User> findAllByIdIsIn(List<Long> longList);
}


直接调用接口操作,这里使用spring boot test来进行测试

package springboot.domain;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import springboot.Application;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)

//// 指定我们SpringBoot工程的Application启动类
@SpringBootTest(classes = Application.class)

///由于是Web项目,Junit需要模拟ServletContext,
///因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration
public class UserDaoTests {
@Autowired
private UserDao userDao;

@Test
public void test() {
SimpleDateFormat sdf = new SimpleDateFormat("yy-mm-dd");
sdf.format(new Date());
//        userDao.save(new User(11L, "jack", "123"));
//        Assert.assertEquals(3, userDao.findAll().size());
System.out.println(userDao.findByUserName("jack").getPassword());
Assert.assertEquals("123", userDao.findByUserName("jack").getPassword());
List<Long> list = new ArrayList<Long>();
list.add(1L);
list.add(2L);
List<User> userList = userDao.findAllByIdIsIn(list);
for (User user : userList){
System.out.println(user.getUserName());
}
}
}


从以上方法中可以看出 ,并没有重写 findAllByIdIsIn方法 但是却实现了想要的效果 这就是JpaRepository接口的效果,根据提示可以拼出各种方法而且可以一眼从方法名中了解到方法的实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring-boot jpa mysql