SpringBoot第 8 讲:SpringBoot+JPA

154 阅读1分钟

开发环境:SpringBoot1.5.2、jdk1.8、MySQL5.7.20 一、创建Maven项目

参考:SpringBoot第 1 讲:HelloWorld_秦毅翔的专栏-CSDN博客

二、修改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>
	<!-- SpringBoot支持01、parent:Begin -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>
	<!-- SpringBoot支持01、parent:End -->
 
	<groupId>org.personal.qin.demos</groupId>
	<artifactId>jps_demo</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<!-- java版本 -->
		<java.version>1.8</java.version>
	</properties>
 
	<dependencies>
		<!-- SpringBoot:Begin -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot:End -->
 
		<!-- SpringMVC:Begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<!-- SpringMVC:End -->
		<!-- MySQL:Begin -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- MySQL:End -->
		
		<!-- JPA:Begin -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<!-- JPA:End -->
 
	</dependencies>
 
	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<!-- 资源文件拷贝插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- SpringBoot支持03、添加SpringBoot的插件支持:Begin -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- SpringBoot支持03、添加SpringBoot的插件支持:End -->
		</plugins>
	</build>
</project>

三、配置数据源

application.properties

server.port = 8080
server.context-path=/
 
#-------------------JPA配置-------------------
spring.datasource.url = jdbc:mysql://10.211.55.4:3306/t_mybatis?characterEncoding=UTF-8&useSSL=false&zeroDateTimeBehavior=convertToNull
spring.datasource.username = root
spring.datasource.password = Aa123123.
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query输入sql语句
spring.jpa.show-sql = true 
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

四、自定义DAO接口

package demo.jpa.dao;
 
import java.util.List;
 
import javax.transaction.Transactional;
 
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
 
import demo.jpa.entity.User;
 
public interface UserDAO extends CrudRepository<User, Integer> { // 实体和实体对应的表主键的类型
 
	public List<User> findAll();
 
	public User findByNameAndPassword(String name, String password);
	
	//源生sql插入
	@Query(value = "insert into t_user values (default, ?1, ?2)", nativeQuery = true)
	@Modifying
	@Transactional
	public int insertUser(String name, String password);
	
	//源生sql 删除
	@Query(value = "delete from t_user where id=?1", nativeQuery = true)
	@Modifying
	@Transactional
	public int deleteUserById(int id);
	
	//源生sql修改
	@Query(value = "update t_user set name=?1, password=?2 where id=?3", nativeQuery = true)
	@Modifying
	@Transactional
	public int updateUser(String name, String password, int id);
}

五、启动BootApplication

package demo.jpa;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import demo.jpa.dao.UserDAO;
import demo.jpa.entity.User;
 
@SpringBootApplication
@RestController
@RequestMapping("user")
public class JpaBootApplication {
 
	@Autowired
	private UserDAO dao;
	
	@GetMapping
	public List<User> test1() {
		return dao.findAll();
	}
 
	@GetMapping("/{name}/{password}")
	public User test2(@PathVariable String name, @PathVariable String password) {
		return dao.findByNameAndPassword(name, password);
	}
 
	@PostMapping("/{name}/{password}")
	public String test3(@PathVariable String name, @PathVariable String password) {
		if (dao.insertUser(name, password) > 0) {
			return "添加用户成功";
		} else {
			return "添加用户失败";
		}
	}
 
	@DeleteMapping("/{id}")
	public String test3(@PathVariable int id) {
		if (dao.deleteUserById(id) > 0) {
			return "删除用户成功";
		} else {
			return "删除用户失败";
		}
	}
 
	@PutMapping("/{id}/{name}/{password}")
	public String test4(@PathVariable int id, @PathVariable String name, @PathVariable String password) {
		if (dao.updateUser(name, password, id) > 0) {
			return "修改用户成功";
		} else {
			return "修改用户失败";
		}
	}
 
	public static void main(String[] args) {
		SpringApplication.run(JpaBootApplication.class, args);
	}
}

六、测试

数据库

浏览器请求

http://127.0.0.1:8080/user