sss
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hnxy</groupId>
<artifactId>sss</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- springboot父级项目,使用它可以直接锁定引入jar包的版本解决版本冲突 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<!-- springBoot核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- springBoot热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- WEB支持主要包括springMVC 和 tomcat插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- jsp相关 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- spring data -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!-- mysqljar -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<!-- 加载springBoot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
# Spring Data DataSource
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql:///lianxissd?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.filters = stat,wall,logback
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.useGlobalDataSourceStat=true
# Spring Data Hibernate
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans = true
# tomcat config
server.port=8080
server.tomcat.uri-encoding=UTF-8
# project name
server.servlet.context-path=/sss
# spring HD
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths= src/main/java
Dao层的创建
1、SpringData 提供了JpaRepository接口,封装了基本的crud操作
2、使用:创建接口 继承JpaRepository接口即可
public interface AccountDao extends
JpaRepository<Account, Integer>
3、JpaRepository<T, ID>
T:操作的实体类的类型
ID:主键的类型
package com.hnxy.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.hnxy.entity.Account;
public interface AccountDao extends JpaRepository<Account, Integer>{
}
Service层的创建
package com.hnxy.service;
import java.util.Map;
import com.hnxy.entity.Account;
public interface AccountService {
//新增和修改
public void saveOrUpdate(Account account) throws Exception;
//删除
public void delete(Account account) throws Exception;
//单个查询
public Account findAccountById(Integer id) throws Exception;
//分页查询
public Map<String,Object> findAccountsByPage(Integer pageIndex, Integer pageSize) throws Exception;
}
Service层的实现类AccountServiceImpl
package com.hnxy.service.impl;
import java.util.HashMap;
import java.util.Map;
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.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import com.hnxy.dao.AccountDao;
import com.hnxy.entity.Account;
import com.hnxy.service.AccountService;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao ;
@Override
public void saveOrUpdate(Account account) throws Exception {
// TODO Auto-generated method stub
accountDao.save(account);
}
@Override
public void delete(Account account) throws Exception {
// TODO Auto-generated method stub
accountDao.delete(account);
}
@Override
public Account findAccountById(Integer id) throws Exception {
// TODO Auto-generated method stub
return accountDao.getOne(id);
}
@Override
public Map<String, Object> findAccountsByPage(Integer pageIndex, Integer pageSize) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
//创建order对象,指定排序规则,参数1:升序或降序,参数2:指定排序的字段
Order order = new Order(Direction.DESC, "id");
//c创建sort对象
Sort sort = Sort.by(order);
//page:是从0开始的页码,自定义的pageIndex是从1开始
//size:每页的记录数
//sort:根据某个阻断进行排序查询
Pageable pageable = PageRequest.of(pageIndex-1, pageSize, sort);
//分页查询
Page<Account> page = accountDao.findAll(pageable);
//封装
map.put("pageIndex", pageIndex);
map.put("pageSize", pageSize);
map.put("totalCount", accountDao.count());
map.put("totalPage", page.getTotalPages());//总页数
map.put("accounts", page.getContent());//每页显示的数据
return map;
}
}
com.hnxy 核心类的创建
com.hnxy.SpringCoreApp 全限定名
创建位置:在其他子包的上一层,比如:com.hnxy
创建springboot核心类:可以向下扫描包中的功能配置,约定大于配置
代码实现
package com.hnxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 创建springboot的核心类
* @author Administrator
*
*/
@SpringBootApplication
public class SpringCoreApp {
public static void main(String[] args) {
SpringApplication.run(SpringCoreApp.class, args);
}
}
web层1
1、之前的web项目中有web.xml配置文件
有欢迎页的配置,当访问项目时,直接访问工程名,就可以找到欢迎页
比如:http://localhost:8080/ssd,可以访问到index.jsp
2、springboot项目没有web.xml配置,没有欢迎页
配置DefaultAction,当请求地址为/时,跳转到index.jsp(首页)
package com.hnxy.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DefaultAction {
@RequestMapping("/")
public String home(){
return "forward:index.jsp";
}
}
web层2 请求和修改 有时间转换
package com.hnxy.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.sql.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hnxy.entity.Account;
import com.hnxy.service.AccountService;
@Controller
public class AccountOperateAction {
@Autowired
private AccountService as;
//处理新增和修改的请求
@RequestMapping("saveOrUpdate")
public String saveOrUpdate(Account account) throws Exception{
as.saveOrUpdate(account);
return "redirect:findAccountsByPage";
}
@RequestMapping("deleteAccount")
public String deleteAccount(Account account) throws Exception{
as.delete(account);
return "redirect:findAccountsByPage";
}
@InitBinder
public void dateBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
}
web层 查询
package com.hnxy.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.hnxy.entity.Account;
import com.hnxy.service.AccountService;
@Controller
public class AccountViewAction {
@Autowired
private AccountService as;
@RequestMapping("findAccountsByPage")
public ModelAndView findAccountsByPage(
@RequestParam(name="pageIndex",required=false,defaultValue="1")Integer pageIndex) throws Exception{
int pageSize = 10;
ModelAndView mv = new ModelAndView();
Map<String, Object> map = as.findAccountsByPage(pageIndex, pageSize);
mv.addObject("map", map);
mv.setViewName("forward:/list.jsp");
return mv;
}
@RequestMapping("findAccountsById")
public ModelAndView findAccountsById(Integer id) throws Exception{
Account account = as.findAccountById(id);
ModelAndView mv = new ModelAndView();
mv.addObject("account", account);
mv.setViewName("forward:/update.jsp");
return mv;
}
}