spring boot+security+jpa+mysql

http://www.360doc.com/content/18/0101/10/30359819_718073321.shtmlbat方法

结构拓扑

在这里插入图片描述

架包 maven

antlr-2.7.7.jar
aspectjweaver-1.9.2.jar
byte-buddy-1.9.5.jar
classmate-1.4.0.jar
dom4j-2.1.1.jar
hibernate-commons-annotations-5.0.4.Final.jar
hibernate-core-5.3.7.Final.jar
hibernate-validator-6.0.13.Final.jar
HikariCP-3.2.0.jar
jackson-annotations-2.9.0.jar
jackson-core-2.9.7.jar
jackson-databind-2.9.7.jar
jackson-datatype-jdk8-2.9.7.jar
jackson-datatype-jsr310-2.9.7.jar
jackson-module-parameter-names-2.9.7.jar
jandex-2.0.5.Final.jar
javassist-3.23.1-GA.jar
javax.activation-api-1.2.0.jar
javax.annotation-api-1.3.2.jar
javax.persistence-api-2.2.jar
javax.transaction-api-1.3.jar
jaxb-api-2.3.1.jar
jboss-logging-3.3.2.Final.jar
jul-to-slf4j-1.7.25.jar
list.txt
log4j-api-2.11.1.jar
log4j-to-slf4j-2.11.1.jar
logback-classic-1.2.3.jar
logback-core-1.2.3.jar
lombok-1.18.4.jar
mysql-connector-java-8.0.13.jar
slf4j-api-1.7.25.jar
snakeyaml-1.23.jar
spring-aop-5.1.3.RELEASE.jar
spring-aspects-5.1.3.RELEASE.jar
spring-beans-5.1.3.RELEASE.jar
spring-boot-2.1.1.RELEASE.jar
spring-boot-autoconfigure-2.1.1.RELEASE.jar
spring-boot-starter-2.1.1.RELEASE.jar
spring-boot-starter-aop-2.1.1.RELEASE.jar
spring-boot-starter-data-jpa-2.1.1.RELEASE.jar
spring-boot-starter-jdbc-2.1.1.RELEASE.jar
spring-boot-starter-json-2.1.1.RELEASE.jar
spring-boot-starter-logging-2.1.1.RELEASE.jar
spring-boot-starter-security-2.1.1.RELEASE.jar
spring-boot-starter-tomcat-2.1.1.RELEASE.jar
spring-boot-starter-web-2.1.1.RELEASE.jar
spring-context-5.1.3.RELEASE.jar
spring-core-5.1.3.RELEASE.jar
spring-data-commons-2.1.3.RELEASE.jar
spring-data-jpa-2.1.3.RELEASE.jar
spring-expression-5.1.3.RELEASE.jar
spring-jcl-5.1.3.RELEASE.jar
spring-jdbc-5.1.3.RELEASE.jar
spring-orm-5.1.3.RELEASE.jar
spring-security-config-5.1.2.RELEASE.jar
spring-security-core-5.1.2.RELEASE.jar
spring-security-web-5.1.2.RELEASE.jar
spring-tx-5.1.3.RELEASE.jar
spring-web-5.1.3.RELEASE.jar
spring-webmvc-5.1.3.RELEASE.jar
tomcat-embed-core-9.0.13.jar
tomcat-embed-el-9.0.13.jar
tomcat-embed-websocket-9.0.13.jar
validation-api-2.0.1.Final.jar
遍历.bat

src

application.properties

#datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/vpsit?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.initialization-mode=always
#jpa
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.user_sql_comments=true
spring.jpa.database-platform=com.tentact.demo.jpa.dialect.UTF8InnodbDialect

src

data.sql

INSERT INTO users(id,user_name,password,description) VALUES(1,'admin','123','I''m administration')
INSERT INTO users(id,user_name,password,description) VALUES(2,'user','123','I''m guest')

INSERT INTO computers(id,brand,cpu_type,memory_size) values(null,'联想','酷睿',8)
INSERT INTO computers(id,brand,cpu_type,memory_size) values(null,'apple','酷睿',16)

INSERT INTO students(id,name,age,computer_id) values(null,'Jack',23,1)

com.tentact.demo

DemoAppliction

package com.tentact.demo;

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

@SpringBootApplication
public class DemoAppliction {

	public static void main(String...args) {
		SpringApplication.run(DemoAppliction.class, args);
	}
}

com.tentact.demo.jpa.dialect

UTF8InnodbDialect

package com.tentact.demo.jpa.dialect;

import org.hibernate.dialect.InnoDBStorageEngine;
import org.hibernate.dialect.MariaDB10Dialect;
import org.hibernate.dialect.MySQLStorageEngine;

public class UTF8InnodbDialect extends MariaDB10Dialect {

	@Override
	protected MySQLStorageEngine getDefaultMySQLStorageEngine() {

		return new InnoDBStorageEngine() {

			@Override
			public String getTableTypeString(String engineKeyword) {
				// TODO Auto-generated method stub
				return String.format("%s=InnoDB DEFAULT CHARSET=utf8", engineKeyword);
			}

		};
	}

}

com.tentact.demo.jpa.entity

UserEntity

package com.tentact.demo.jpa.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
//javax包下的方法
@Entity
@Table(name = "users")
public class UserEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)//MySQL中有用
	private Long id;

	@Column(name = "user_name", nullable = false, length = 100)
	private String name;
	@Column(name = "password", nullable = true, length = 100)
	private String password;

	private String description;

	public Long getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

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

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}

StudentEntity 一对一 从表

package com.tentact.demo.jpa.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "students")
public class StudentEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String name;
	private Integer age;
	@OneToOne//一对一
	private ComputerEntity computer;//主表

	public Long getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public ComputerEntity getComputer() {
		return computer;
	}

	public void setComputer(ComputerEntity computer) {
		this.computer = computer;
	}

}

ComputerEntity 一对一 主表

package com.tentact.demo.jpa.entity;

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 = "computers")//主表
public class ComputerEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(name = "brand", nullable = false, length = 50)//控制列
	private String brand;

	@Column(name = "cpu_type", nullable = false, length = 50)
	private String cpuType;

	@Column(name = "memory_size")
	private Integer memorySize;

	public Long getId() {
		return id;
	}

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

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getCpuType() {
		return cpuType;
	}

	public void setCpuType(String cpuType) {
		this.cpuType = cpuType;
	}

	public Integer getMemorySize() {
		return memorySize;
	}

	public void setMemorySize(Integer memorySize) {
		this.memorySize = memorySize;
	}

}

com.tentact.demo.jpa.repository

UserEntityRepository

package com.tentact.demo.jpa.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.tentact.demo.jpa.entity.UserEntity;
import com.tentact.demo.vo.User;

public interface UserEntityRepository extends JpaRepository<UserEntity, Long> {

	// List<User> findByName (String name);//判断name等于什么什么东西
	List<User> findByNameLike(String name);// 判断name like什么什么东西

	List<UserEntity> findByNameContainingAndPassword(String name, String password);

	@Modifying
	@Query("DELETE FROM UserEntity WHERE name=:name") // JPQL
	int deleteByName(@Param("name") String name);

	@Modifying
	@Query("UPDATE UserEntity set password=:newPassword WHERE id=:id") // JPQL
	int updatePassword(@Param("id") Long id, @Param("newPassword") String password);
}

StudentEntityRepository

package com.tentact.demo.jpa.entity;

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 = "computers")//主表
public class ComputerEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(name = "brand", nullable = false, length = 50)//控制列
	private String brand;

	@Column(name = "cpu_type", nullable = false, length = 50)
	private String cpuType;

	@Column(name = "memory_size")
	private Integer memorySize;

	public Long getId() {
		return id;
	}

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

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getCpuType() {
		return cpuType;
	}

	public void setCpuType(String cpuType) {
		this.cpuType = cpuType;
	}

	public Integer getMemorySize() {
		return memorySize;
	}

	public void setMemorySize(Integer memorySize) {
		this.memorySize = memorySize;
	}

}

com.tentact.demo.service

StudentService

package com.tentact.demo.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tentact.demo.jpa.entity.StudentEntity;
import com.tentact.demo.jpa.repository.StudentEntityRepository;
import com.tentact.demo.vo.Computer;
import com.tentact.demo.vo.Student;

@Service
public class StudentService {

	@Autowired
	private StudentEntityRepository studentEntityRepository;

	public Student findById(Long id) {
		StudentEntity studentEntity = this.studentEntityRepository.getOne(id);
		Computer computer = new Computer();
		BeanUtils.copyProperties(studentEntity.getComputer(), computer);
		Student student = new Student();
		BeanUtils.copyProperties(studentEntity, student);
		student.setComputer(computer);
		return student;
	}
}

UserService

package com.tentact.demo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tentact.demo.jpa.entity.UserEntity;
import com.tentact.demo.jpa.repository.UserEntityRepository;
import com.tentact.demo.vo.User;

@Service
@Transactional
public class UserService {
	@Autowired
	private UserEntityRepository userEntityRepository;

	public List<User> findAll() {
		List<UserEntity> userEntities = this.userEntityRepository.findAll();
		List<User> users = new ArrayList<>();
		userEntities.stream().forEach(u -> {
			User temp = new User();
			BeanUtils.copyProperties(u, temp);
			users.add(temp);
		});
		return users;
	}

	public List<User> findByName(String name, String password) {
		List<UserEntity> userEntities = this.userEntityRepository.findByNameContainingAndPassword(name, password);

		List<User> users = new ArrayList<>();

		userEntities.stream().forEach(u -> {
			User temp = new User();
			BeanUtils.copyProperties(u, temp);
			users.add(temp);
		});
		return users;
	}

	public User save(User user) {
		UserEntity userEntity = new UserEntity();
		BeanUtils.copyProperties(user, userEntity);
		userEntity = this.userEntityRepository.save(userEntity);
		BeanUtils.copyProperties(userEntity, user);
		return user;
	}
	//根据id删除
	public void delete(Long id) {
		this.userEntityRepository.deleteById(id);
	}
	//根据名字删除
	public int deleteByName(String name) {
		return this.userEntityRepository.deleteByName(name);
	}
	//更新update
	public void updatePassword(Long id, String newPassword) {
		this.userEntityRepository.updatePassword(id, newPassword);

	}
}

com.tentact.demo.vo

User

package com.tentact.demo.vo;

public class User {
	private Long id;

	private String name;
	private String password;

	private String description;

	public Long getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

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

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}


Student

package com.tentact.demo.vo;

public class Student {
	private Long id;
	private String name;
	private Integer age;
	private Computer computer;

	public Long getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Computer getComputer() {
		return computer;
	}

	public void setComputer(Computer computer) {
		this.computer = computer;
	}
}

Computer

package com.tentact.demo.vo;

public class Computer {

	private Long id;
	private String brand;
	private String cpuType;
	private Integer memorySize;

	public Long getId() {
		return id;
	}

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

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getCpuType() {
		return cpuType;
	}

	public void setCpuType(String cpuType) {
		this.cpuType = cpuType;
	}

	public Integer getMemorySize() {
		return memorySize;
	}

	public void setMemorySize(Integer memorySize) {
		this.memorySize = memorySize;
	}

}

com.tentact.demo.controller

StudentController

package com.tentact.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tentact.demo.service.StudentService;
import com.tentact.demo.vo.Student;

@RestController
public class StudentController {

	@Autowired
	private StudentService studentService;

	@RequestMapping("/findById")
	public Student findById(Long id) {
		return this.studentService.findById(id);
	}
}

UserController

package com.tentact.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tentact.demo.service.UserService;
import com.tentact.demo.vo.User;

@RestController
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/f")
	public List<User> findAll() {
		return this.userService.findAll();
	}

	@RequestMapping("/findByName")
	public List<User> findByName(String name, String password) {
		return this.userService.findByName(name, password);
	}

	@RequestMapping("/save")
	public User save(User user) {
		System.out.println(user.getName());
		return this.userService.save(user);
	}

	@RequestMapping("/delete")
	public String delete(Long id) {
		this.userService.delete(id);
		return "success";
	}

	@RequestMapping("/deleteByName")
	public Integer deleteByName(String name) {
		return this.userService.deleteByName(name);
	}

	@RequestMapping("/update")
	public String update(Long id, String newPassword) {
		this.userService.updatePassword(id, newPassword);
		return "success";
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值