目录
在springboot 配置多个模块,难点就在 每一个模块应该怎么配置 pom,
根目录pom
新建一个 spring initialization 项目,取名为 ch22
这个总工程的 pom.xml,你只需要把 机器生成的代码 ,替换几个就行:
* 注意 这里的 packaging 是 pom*
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>common</module>
<module>web</module>
</modules>
<!--引入web依赖-->
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
新建common
记住: 点击工程,右键按 new–》 mudole
取名: common
这个 模块的 pom必要的元素:
<parent>
<artifactId>ch22</artifactId>
<groupId>com.bz</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
新建web
重点: new module 选择的是 spring inilation,
这里是打的war
<groupId>com.bz</groupId>
<artifactId>web</artifactId>
<version>10.0</version>
<packaging>war</packaging>
<name>web</name>
<parent>
<groupId>com.bz</groupId>
<artifactId>ch22</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<!--引入用户模块-->
<dependency>
<groupId>com.bz</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
1.在这个 web中 新建 webapp –WEB-INF– index.jsp
2.在 application.yml中 添加
方便实现跳转
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
3.在java根下新建文件
方便能在外部 tomcat中启动
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(PagerApplication.class);
}
}
4.在java文件名字根启动类 同级新建一个包 controller
再新建一个类:
@Controller
public class maintestController {
@RequestMapping(value = "/index")
public String index(HttpServletRequest request)
{
request.setAttribute("name","sdfasdf");
return "index";
}
}
配置orm—-datajpa
1.首先在 pom中引入这些
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.10.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
yml中配置数据库地址和选择相应的环境
spring:
jpa:
database: POSTGRESQL
show-sql: true
hibernate.ddl-auto: update
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Chongqing
database:
driverClassName: org.postgresql.Driver
profiles:
active: dev
application-dev.yml
spring:
datasource:
platform: postgres
url: jdbc:postgresql://url
username: root1234
password: root1234
profiles: dev
- 新建一个配置类,用来映射 Resopity和实体类
@Configuration
@EnableJpaRepositories(basePackages = "com.example.pttest.dao")
@EnableAutoConfiguration
@EntityScan(basePackages = {"com.example.pttest.entity"})
public class JpaConfig {
}
3.新建 dao层
原生sql+jhql
传值方式+事务控制+分页
public interface userRepository extends JpaRepository<user, Long>,PagingAndSortingRepository<user, Long> {
@Query("select u from user u where u.id=:sex")
public List<user> getiones(@Param("sex") Long sex);
@Query("select u from user u where u.name=?1")
public List<user> getones(String sex);
@Query(nativeQuery = true, value ="select * from tbl_user u where u.name=?1")
public List<user> yuansheng(String sex);
@Query(nativeQuery = true,
value ="select u.name,b.bonus_id from tbl_user u,tbl_bonus_pool_info b where u.id=b.bonus_id ")
public List<Object> findByCity();
@Query("select u from user u ")
public Page<user> getUsersBySexasdf(Pageable pageable);
//控制事务
@Modifying
@Query(" update user a set a.id=?1 where a.name=?2 ")
public void shiwude(Long id,String name);
@Query("select u from user u ")
public Page<user> getPagenum(Pageable pageable);
}
4.jpa多表查询,结果集是杂乱的
@NoRepositoryBean 禁止被注入成
@NoRepositoryBean
public interface qurryCommon {
public List dosomething(String name);
PageData getProductCombo(int pageSize, int pageIndex, String name);
}
然后实现它
注意传值方式
@Repository
@Transactional
public class queryComonImpl extends BaseRepository implements qurryCommon {
@Override
public List dosomething(String name) {
String sql = " select * from tbl_user a where 1=1 " +
" and a.name = :name " ;
Map params = Maps.newHashMap();
params.put("name", name);
return this.getList(sql, params);
}
@Override
public PageData getProductCombo(int pageSize, int pageIndex, String name) {
String sql = " select * from tbl_user a where 1=1 ";
Map params = Maps.newHashMap();
if (!Strings.isNullOrEmpty(name)) {
sql += " and a.name like :name \n";
params.put("name", "%" + name + "%");
}
sql += "order by a.id desc " ;
return this.getPage(sql, params, pageSize, pageIndex);
}
}
底层的 分页类
使用的是pgsql
pgsql分页: “%s limit %d offset %d”, sqlStr, pageSize, pageIndex * pageSize),
mysql分页: “%s limit %d %d”, sqlStr, pageIndex * pageSize,pageSize ),
public abstract class BaseRepository {
@PersistenceContext
protected EntityManager entities;
protected <T> T getScalar(String sql, Class<T> clazz) {
return this.getScalar(sql, Maps.newHashMap(), clazz);
}
protected <T> T getScalar(String sql, Map params, Class<T> clazz) {
List list = this.getList(sql, params);
if (list.size() == 0) {
return null;
}
Map first = (Map) Iterables.get(list, 0);
if (first.entrySet().size() == 0) {
return null;
}
Map.Entry entry = (Map.Entry) Iterables.get(first.entrySet(), 0);
return (T) entry.getValue();
}
protected List getList(String sqlStr) {
return this.getList(sqlStr, Maps.newHashMap());
}
protected List getList(String sqlStr, Map params) {
Query query = this.entities.createNativeQuery(sqlStr);
if (params != null) {
for (Object key : params.keySet()) {
query.setParameter((String) key, params.get(key));
}
}
return this.toMap(query);
}
protected List getEntityList(String sqlStr, Map params) {
Query query = this.entities.createQuery(sqlStr);
if (params != null) {
for (Object key : params.keySet()) {
query.setParameter((String) key, params.get(key));
}
}
return query.getResultList();
}
protected int getCount(String sqlStr) {
return this.getCount(sqlStr, Maps.newHashMap());
}
protected int getCount(String sqlStr, Map params) {
Query query = this.entities.createNativeQuery(sqlStr);
if (params != null) {
for (Object key : params.keySet()) {
query.setParameter((String) key, params.get(key));
}
}
BigInteger result = (BigInteger) query.getSingleResult();
return result.intValueExact();
}
protected PageData getPage(String sqlStr, int pageSize, int pageIndex) {
return this.getPage(sqlStr, Maps.newHashMap(), pageSize, pageIndex);
}
protected PageData getPage(String sqlStr, Map params, int pageSize, int pageIndex) {
List data = null;
int total = 0;
String totalCount = "totalCount";
if(params.containsKey(totalCount)){
total = Integer.parseInt(params.get(totalCount).toString());
params.remove(totalCount);
}
if (pageSize == 0 && pageIndex == 0){
data = this.getList(sqlStr,params);
}else{
data = this.getList(String.format("%s limit %d offset %d", sqlStr, pageSize, pageIndex * pageSize), params);
}
//优化分页查询
if(total == 0){ //重新计算中总条数
total = this.getCount(String.format("select count(1) as rows from (%s) as q", sqlStr), params);
}
return new PageData(data, total);
}
private List toMap(Query query) {
if (query instanceof HibernateQuery) {
query.unwrap(HibernateQuery.class)
.getHibernateQuery()
.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
return query.getResultList();
}
throw new IllegalStateException();
}
private void addScalar(Query query, String column, Type type) {
if (query instanceof HibernateQuery) {
query.unwrap(SQLQuery.class).addScalar(column, type);
}
throw new IllegalStateException();
}
}