最近公司领导要求让用SpringBoot+JPA进行项目开发。所以安排我搭建项目框架。

第一步.POM文件的配置
<parent> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version> </parent> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <!--去除 spring-boot-starter-web
自带的下方jar包--> <exclusions> <exclusion> <artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId> </exclusion> <exclusion>
<artifactId>org.springframework.boot</artifactId>
<groupId>spring-boot-starter-logging</groupId> </exclusion> </exclusions>
</dependency> <!--jpa 相关jar包 --> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySql
相关JAR包 --> <dependency> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> </dependency>
<!--spring-boot整合redis包--> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
<!--添加spring整合shiro的jar包--> <dependency> <groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId> <version>1.3.2</version> </dependency>
<dependency> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> </dependency> <!--JSON.toJSONString()依赖的JSON
jar包--> <dependency> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <version>1.2.37</version> </dependency> <!--
httpClient的jar包 --> <dependency> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> </dependency> <!--log4j依赖--> <dependency>
<groupId>log4j</groupId> <artifactId>log4j</artifactId>
<version>1.2.17</version> </dependency> <!--log4j2日志依赖--> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- 热部署的jar包
--> <dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> <optional>true</optional>
</dependency> <dependency> <groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId> <version>2.6.1</version>
</dependency> <dependency> <groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version>
</dependency> </dependencies> <!-- 添加SpringBoot热部署插件 --> <build> <plugins>
<plugin> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId> <dependencies>
<!--springloaded hot deploy --> <dependency>
<groupId>org.springframework</groupId> <artifactId>springloaded</artifactId>
<version>1.2.4.RELEASE</version> </dependency> </dependencies> <executions>
<execution> <goals> <goal>repackage</goal> </goals> <configuration>
<classifier>exec</classifier> </configuration> </execution> </executions>
</plugin> </plugins> </build>
第二步骤.

application.properties配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true #数据库方言
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
######################################################## ###Redis (Redis配置)
########################################################
#设置使用哪一个redis数据库【redis默认有16个数据库0-15】 spring.redis.database=1 #redis主机IP
spring.redis.host=127.0.0.1 #redis端口 spring.redis.port=6379 #redis密码
#spring.redis.password= spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1 #设置客户端闲置5s后关闭连接 spring.redis.timeout=5000
第三步.

创建实体类。
import javax.persistence.Column; import javax.persistence.Entity; import
javax.persistence.GeneratedValue; import javax.persistence.Id; import
javax.persistence.Table; @Entity @Table(name="user2") public class User2 { @Id
@GeneratedValue private Integer id; @Column(name="name") private String name;
@Column(name="password") private String password; public Integer getId() {
return id; } public void setId(Integer 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 User2(String
name,String password) { this.password = password; this.name = name; } @Override
public String toString() { return "User2 [id=" + id + ", name=" + name + ",
password=" + password + "]"; } public User2() { } }
第四步。创建Dao层集成JpaRepository
import java.util.List; import org.springframework.data.domain.Page; import
org.springframework.data.domain.Pageable; import
org.springframework.data.jpa.repository.JpaRepository; import
org.springframework.data.jpa.repository.Modifying; import
org.springframework.data.jpa.repository.Query; public interface User2JpaDao
extends JpaRepository<User2, Long> { /* * Jpa命名规范,查询 */ User2
findByNameAndPasswordAndId(String name,String password,Integer id); /* *
(non-Javadoc)新增用户 * @see
org.springframework.data.repository.CrudRepository#save(S) */ User2 save(User2
user2); //查询全部 List<User2> findAll(); //分页查询 //Page<User2> findAll(PageRequest
pageRequest); Page<User2> findAll(Pageable pageable); @Modifying @Query("update
User2 as c set c.name = ?1 where c.password=?2") int
updateNameByPassword(String name, String password); void delete(User2 entity); }
第五步。创建Service层。
import java.util.List; import org.springframework.data.domain.Page; import
org.springframework.data.domain.Pageable; public interface User2Service { User2
findByNameAndPasswordAndId(String name,String password,Integer id); User2
save(User2 user2); List<User2> findAll(); int updateNameByPassword(String name,
String password); //Page<User2> findAll(PageRequest pageRequest); Page<User2>
findAll(Pageable pageable); //删除用户 void delete(User2 entity); }

第六步。创建ServiceImpl层。

import java.util.List; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.data.domain.Page; import
org.springframework.data.domain.Pageable; import
org.springframework.stereotype.Service; @Service public class User2ServiceImpl
implements User2Service { @Autowired private User2JpaDao user2JpaDao; @Override
public User2 findByNameAndPasswordAndId(String name, String password, Integer
id) { // TODO Auto-generated method stub return
user2JpaDao.findByNameAndPasswordAndId(name, password,id); } @Override public
User2 save(User2 user2) { // TODO Auto-generated method stub return
user2JpaDao.save(user2); } @Override public List<User2> findAll() { // TODO
Auto-generated method stub return user2JpaDao.findAll(); } @Override public int
updateNameByPassword(String name, String password) { // TODO Auto-generated
method stub return user2JpaDao.updateNameByPassword(name, password); }
@Override public void delete(User2 entity) { // TODO Auto-generated method stub
user2JpaDao.delete(entity); } @Override public Page<User2> findAll(Pageable
pageable) { // TODO Auto-generated method stub return
user2JpaDao.findAll(pageable); } }
第七步 controller层
import java.util.ArrayList; import java.util.List; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.data.domain.Page; import
org.springframework.data.domain.PageRequest; import
org.springframework.data.domain.Pageable; import
org.springframework.data.domain.Sort; import
org.springframework.web.bind.annotation.RequestMapping; import
org.springframework.web.bind.annotation.RestController; @RestController public
class User2Controller { @Autowired private User2Service user2Service;
@Autowired private RedisService redisService; @RequestMapping("/getRedis")
public String getRedis(){ String string2="zouyunke111111";
redisService.set(string2, "nihao"); String string=(String)
redisService.get("zouyunke111111"); return string; }
@RequestMapping("/findByNameAndPasswordAndId") public User2
findByNameAndPassword(){ User2
user2=user2Service.findByNameAndPasswordAndId("zouyunke", "yunke123", 3);
//User2 user2=user2Service.findByNameAndPwdAndId("zouyunke", "yunke123",1);
return user2; } @RequestMapping("/saveUser2") public List<User2> saveUser2(){
List<User2> user3=new ArrayList<User2>(); for (int i=0;i<10;i++){ User2
user2=new User2("zouyunke12"+i,"zouyunke12"+i); user3.add(user2); } for (User2
user2 : user3) { User2 user=user2Service.save(user2); } //User2
user2=user2Service.findByNameAndPwdAndId("zouyunke", "yunke123",1); return
user2Service.findAll(); } @RequestMapping("/updateUser2") public User2
updateUser2(){ User2 user2=new User2(); user2.setId(1);
user2.setName("zhaodanya"); user2.setPassword("123"); return
user2Service.save(user2); } /** * 分页查询 * @author Administrator * 2018年4月22日 *
@return * TODO */ @RequestMapping("/deleteUser2") public void deleteUser2(){
User2 user2=new User2(); user2.setId(3); user2.setName("zouyunke120");
user2.setPassword("zouyunke120"); user2Service.delete(user2); }
@RequestMapping(value = "/pageUser2") public Page<User2> pageUser2() { Sort
sort = new Sort(Sort.Direction.ASC,"id"); Pageable pageable = new
PageRequest(2,6,sort); Page<User2> page = user2Service.findAll(pageable);
return page; } }
至此,整合完毕。

上面写的都是jpa的东西。再写一点和jpa无关的东西。

自定义sql语句,由于项目的进展,需要一些自定义sql语句,所以封装了一个dao和一个实现类来自定义sql语句。





package cn.dao.base; import java.util.List; import java.util.Map; /** * 类说明:
原生SQL操作数据库的Dao * @author 邹运坷 * @version 创建时间:2018年4月26日 下午12:10:26 */ public
interface BaseDao { /** * 公共查询方法【手动进行参数的拼接】 * @return */ List<Map>
commonQueryMethod(String sql); /** * 增删改共用方法【手动进行参数的拼接】 * @param sql */ int
addOrDelOrUpdateMethod(String sql); /** *
公共查询方法【传递参数集合自动绑定参数】参数集合中的key要和SQL中的命名参数名称一致 * @return */ List<Map>
commonQueryMethod(String sql,Map<String,Object> param); /** *
增删改共用方法【传递参数集合自动绑定参数】参数集合中的key要和SQL中的命名参数名称一致 * @param sql */ int
addOrDelOrUpdateMethod(String sql,Map<String,Object> param); }package
cn.dao.base.impl; import java.util.List; import java.util.Map; import
javax.persistence.EntityManager; import javax.persistence.PersistenceContext;
import org.hibernate.Criteria; import org.hibernate.Query; import
org.hibernate.Session; import org.springframework.stereotype.Repository; import
cn.yuanyue.ycmusic.dao.base.BaseDao; /** * 类说明:BaseDao实现类 * @author 邹运坷 *
@version 创建时间:2018年4月26日 下午12:13:18 */ @Repository public class BaseDaoImpl
implements BaseDao { @PersistenceContext private EntityManager entityManager;
/** * 获取HibernateSession */ private Session getHibernateSession() {
//获取Hibernate中的Session Session hibernateSession =
entityManager.unwrap(org.hibernate.Session.class); return hibernateSession; }
/** * 公共查询方法【手动拼接参数到SQL中】 * @param sql */ @Override public List<Map>
commonQueryMethod(String sql) { //执行SQL查询【设置返回结果为Map】 Query result =
getHibernateSession().createSQLQuery(sql).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
return result.list(); } /** * 增删改共用方法【手动拼接参数到SQL中】 * @param sql */ @Override
public int addOrDelOrUpdateMethod(String sql) { Query result =
getHibernateSession().createSQLQuery(sql); int executeUpdate =
result.executeUpdate(); return executeUpdate; } /** *
公共查询方法【传递参数集合自动绑定参数】参数集合中的key要和SQL中的命名参数名称一致 * select * from xx where id = :key
* put("key",'1') * @return */ @Override public List<Map>
commonQueryMethod(String sql, Map<String, Object> param) { Query result =
getHibernateSession().createSQLQuery(sql).setProperties(param).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
return result.list(); } /** * 增删改共用方法【传递参数集合自动绑定参数】参数集合中的key要和SQL中的命名参数名称一致 *
@param sql */ @Override public int addOrDelOrUpdateMethod(String sql,
Map<String, Object> param) { Query result =
getHibernateSession().createSQLQuery(sql).setProperties(param); int
executeUpdate = result.executeUpdate(); return executeUpdate; } }
上面就是自定义sql的源码。一个dao一个实现类。使用的时候只需要在serviceimpl里注入就行了。

注入方式如下


@Autowired
private BaseDao baseDao;

String sql="select name from user";


List<Map>result=baseDao.commonQueryMethod(sql);

这样就实现了自定义sql语句的问题。

如有问题,请联系qq102528071









源码下载地址:点击打开链接 <https://download.csdn.net/download/m0_37256801/10367233>

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信