博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring boot for Eclipse 开发指南第二节 JPA
阅读量:7134 次
发布时间:2019-06-28

本文共 5871 字,大约阅读时间需要 19 分钟。

hot3.png

首先给出pom.xml 其中包括了必须的jpa依赖 log4j2依赖 mysql 和 jdbc 等等

4.0.0
com.avicsafety
webapp
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.4.0.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-starter-log4j2
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-tomcat
org.slf4j
slf4j-log4j2
com.alibaba
druid
1.0.18
org.springframework.boot
spring-boot-maven-plugin
repackage
exec
maven-compiler-plugin
1.8
1.8

 

创建实体类

package com.avicsafety.webapp.model;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class User {    @Id    @GeneratedValue    private Long id;    @Column(nullable = false)    private String name;    @Column(nullable = false)    private Integer age;}

 

数据访问层

package com.avicsafety.webapp.dao;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.query.Param;import com.avicsafety.webapp.model.User;public interface UserRepository extends JpaRepository
{ User findByName(String name); User findByNameAndAge(String name, Integer age); @Query("from User u where u.name=:name") User findUser(@Param("name") String name);}

 

逻辑层的接口和实现

package com.avicsafety.webapp.service;import com.avicsafety.webapp.model.User;public interface IUserService {	public void AddUser(User user);}

 

package com.avicsafety.webapp.service.impl;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.avicsafety.webapp.dao.UserRepository;import com.avicsafety.webapp.model.User;import com.avicsafety.webapp.service.IUserService;@Servicepublic class UserServiceImpl implements IUserService {		private static final Log logger = LogFactory.getLog(UserServiceImpl.class);		@Autowired	UserRepository dao;	@Override	public void AddUser(User user) {		// TODO Auto-generated method stub		dao.save(user);		logger.info("add user");	}}

 

控制层 

package com.avicsafety.webapp;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.avicsafety.webapp.model.User;import com.avicsafety.webapp.service.IUserService;@RestController@EnableAutoConfiguration@SpringBootApplication  public class MyApplication {		@Autowired   	private IUserService userService;	 	@RequestMapping("/")    String home() { 		User user = new User(); 		user.setName("shili"); 		user.setAge(11); 		userService.AddUser(user);        return "ok";    } 	    public static void main(String[] args) throws Exception {        SpringApplication.run(MyApplication.class, args);    }}

 

application.properties 配置

spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://127.0.0.1/mydb?characterEncoding=utf8&zeroDateTimeBehavior=convertToNullspring.datasource.username = rootspring.datasource.password = #####spring.jpa.database = MYSQLspring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialectspring.jpa.show-sql = truespring.jpa.hibernate.ddl-auto = updatespring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategystrategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpllogging.config=classpath:log4j2.xmlspring.devtools.restart.enabled = true

log4j2.xml 配置

 

转载于:https://my.oschina.net/u/659068/blog/1545978

你可能感兴趣的文章
VML/SVG开发配电站接线系统
查看>>
Oracle 数据库导入导出 dmp文件
查看>>
浅谈什么是云主机及其优势所在
查看>>
使用命令行工具对LSI阵列卡进行高效管理
查看>>
利用Java编码实现对oracle数据库的操作
查看>>
java字符串分割处理split及特殊符号
查看>>
远程连接mysql慢
查看>>
我的友情链接
查看>>
Linux学习进阶路线图
查看>>
Java多线程编程之限制优先级
查看>>
linux系统中如何进入退出vim编辑器使用方法
查看>>
8. 比权量力-chmod,chown,umask,lsattr,chattr命令
查看>>
Jenkins RCE CVE-2019-1003000 漏洞复现
查看>>
NumberFormat和DecimalFormat
查看>>
PreferenceActivity
查看>>
Linux系统之系统简介
查看>>
快速排序(JAVA)
查看>>
mysql视图
查看>>
table 水平居中
查看>>
for 循环用法
查看>>