SpringBoot Hibernate 增删改查详解编程语言

1、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; 
 
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; 
 
@Entity 
@Table(name="t_d_user") 
public class User extends JpaRepositoriesAutoConfiguration { 
	 
	@Id 
    @GeneratedValue(strategy= GenerationType.IDENTITY) 
    private long id; 
 
	@Column(name="f_user_name", length=20, nullable=false) 
    private String f_user_name; 
 
	@Column(name="f_user_pwd", length=100, nullable=false) 
    private String f_user_pwd; 
     
	@Column(name="f_user_position", length=100, nullable=false) 
    private String f_user_position; 
     
	@Column(name="f_register_date", length=100, nullable=false) 
    private String f_register_date; 
     
	@Column(name="f_last_date", length=100, nullable=false) 
    private String f_last_date; 
     
	@Column(name="f_last_location", length=100, nullable=false) 
    private String f_last_location; 
     
	@Column(name="f_lock_state", length=100, nullable=false) 
    private String f_lock_state; 
 
	public long getId() { 
		return id; 
	} 
 
	public void setId(long id) { 
		this.id = id; 
	} 
 
	public String getF_user_name() { 
		return f_user_name; 
	} 
 
	public void setF_user_name(String f_user_name) { 
		this.f_user_name = f_user_name; 
	} 
 
	public String getF_user_pwd() { 
		return f_user_pwd; 
	} 
 
	public void setF_user_pwd(String f_user_pwd) { 
		this.f_user_pwd = f_user_pwd; 
	} 
 
	public String getF_user_position() { 
		return f_user_position; 
	} 
 
	public void setF_user_position(String f_user_position) { 
		this.f_user_position = f_user_position; 
	} 
 
	public String getF_register_date() { 
		return f_register_date; 
	} 
 
	public void setF_register_date(String f_register_date) { 
		this.f_register_date = f_register_date; 
	} 
 
	public String getF_last_date() { 
		return f_last_date; 
	} 
 
	public void setF_last_date(String f_last_date) { 
		this.f_last_date = f_last_date; 
	} 
 
	public String getF_last_location() { 
		return f_last_location; 
	} 
 
	public void setF_last_location(String f_last_location) { 
		this.f_last_location = f_last_location; 
	} 
 
	public String getF_lock_state() { 
		return f_lock_state; 
	} 
 
	public void setF_lock_state(String f_lock_state) { 
		this.f_lock_state = f_lock_state; 
	} 
} 

 

2、Dao

 
import org.springframework.data.repository.CrudRepository; 
import org.springframework.stereotype.Repository; 
 
import com.wec.light.mq.center.entity.User; 
 
@Repository 
public interface UserDao extends CrudRepository<User, Long> {     
     
}

 

3、Service

 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
 
import com.wec.light.mq.center.dao.UserDao; 
import com.wec.light.mq.center.entity.User; 
 
@Service 
public class UserService { 
 
	@Autowired 
    private UserDao userDao; 
 
	public void save(User user) { 
		userDao.save(user); 
	} 
	 
	public Iterable<User> findAll() { 
		return userDao.findAll(); 
	} 
	 
	public void updateById(User user) { 
		userDao.save(user); 
	} 
	 
	public void delete(long id) { 
		userDao.deleteById(id); 
	} 
}

 

 

4、Controller

 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
 
import javax.jms.Destination; 
 
import org.apache.activemq.command.ActiveMQQueue; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.CommandLineRunner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.bind.annotation.RestController; 
 
import com.wec.light.mq.center.entity.User; 
import com.wec.light.mq.center.producer.MqCenterProducer; 
import com.wec.light.mq.center.service.UserService; 
 
@RestController 
@SpringBootApplication 
public class MqCenterApplication implements CommandLineRunner { 
 
	@Autowired 
	private MqCenterProducer producer; 
 
	@Autowired 
	private UserService userService; 
 
	public static void main(String[] args) { 
		SpringApplication.run(MqCenterApplication.class, args); 
	} 
 
	@RequestMapping("/") 
	public String hello() { 
		return "Hello world!"; 
	} 
 
	@RequestMapping("/save") 
	@ResponseBody 
	public void save(@RequestParam(value = "name") String name,  
			@RequestParam(value = "password") String password, 
			@RequestParam(value = "position") String position, 
			@RequestParam(value = "registerDate") String registerDate, 
			@RequestParam(value = "lastDate") String lastDate, 
			@RequestParam(value = "lastLocation") String lastLocation, 
			@RequestParam(value = "lockState") String lockState) { 
		User user = new User(); 
		user.setF_user_name(name); 
		user.setF_user_pwd(password); 
		user.setF_user_position(position); 
		user.setF_register_date(registerDate); 
		user.setF_last_date(lastDate); 
		user.setF_last_location(lastLocation); 
		user.setF_lock_state(lockState); 
		userService.save(user); 
	} 
	 
	@RequestMapping("/findAll") 
	@ResponseBody 
	public Iterable<User> save() { 
		return userService.findAll(); 
	} 
	 
	@RequestMapping("/update") 
	@ResponseBody 
	public void save(@RequestParam(value = "id") long id,  
			@RequestParam(value = "name") String name,  
			@RequestParam(value = "password") String password, 
			@RequestParam(value = "position") String position, 
			@RequestParam(value = "registerDate") String registerDate, 
			@RequestParam(value = "lastDate") String lastDate, 
			@RequestParam(value = "lastLocation") String lastLocation, 
			@RequestParam(value = "lockState") String lockState) { 
		User user = new User(); 
		user.setId(id); 
		user.setF_user_name(name); 
		user.setF_user_pwd(password); 
		user.setF_user_position(position); 
		user.setF_register_date(registerDate); 
		user.setF_last_date(lastDate); 
		user.setF_last_location(lastLocation); 
		user.setF_lock_state(lockState); 
		userService.updateById(user); 
	} 
	 
	@RequestMapping("/delete") 
	@ResponseBody 
	public void delete(@RequestParam(value = "id") long id) { 
		userService.delete(id); 
	} 
 
	@Override 
	public void run(String... args) throws Exception { 
 
	} 
}

 

5、配置:

 
# datasource 
spring.datasource.url = jdbc:mysql://localhost:3306/busymonkey_db 
spring.datasource.username = houjiaqi 
spring.datasource.password = 1111 
spring.datasource.driverClassName = com.mysql.jdbc.Driver 
 
# JPA 
spring.jpa.database = MYSQL 
spring.jpa.show-sql = true 
spring.jpa.hibernate.ddl-auto = update 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

 

6、依赖

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

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16595.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论