(1)Spring的包,包含了Spring MVC的包
(2)SpringAOP的插件包
(3)响应JSON类型的视图信息
(4)SpringMVC响应JSON数据的依赖包
(5)Mybatis和JDBC
(6)C3P0连接池和依赖包
(7)SpringMVC 后端参数校验的jar包
(8)JDBC连接数据库的jar包
(1)加载Spring需要在web.xml中加载Spring的监听器
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
(2)根据配置在src目录下新建applicationContext.xml
文件,在文件中定义好schema校验
(1)在web.xml中配置核心控制器
spring-mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
spring-mvc
/
(2)在web.xml中加载Spring提供的字符编码过滤器
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/
(3)在web.xml中配置PUT提交方式的参数处理过滤器
HttpMethodFilter
org.springframework.web.filter.HttpPutFormContentFilter
HttpMethodFilter
/
(4)在src下新建springmvc.xml
文件
(5)如果项目中需要做文件上传,需要在springmvc的配置文件中定义多部件处理器
整合 mybatis 就像将原本由 Mybatis 负责创建的数据层代理对象委托给Spring来创建,所以我们必须提供数据源、SqlSessionFactory,以及一些 mybatis 的属性。
(1)配置C3P0数据源
先在src下配置一个jdbc.properties
资源文件,用来描述连接数据库的信息以及c3p0的常用信息
#jdbc 连接信息
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl = jdbc:mysql://localhost:3306/ebuy?useUnicode=true&characterEncoding=utf-8
jdbc.user = root
jdbc.password =
#c3p0 信息
c3p0.minPoolSize = 10
c3p0.maxPoolSize = 50
c3p0.initialPoolSize = 20
c3p0.maxIdleTime = 60
c3p0.acquireIncrement = 10
c3p0.checkoutTimeout=6000
c3p0.idleConnectionTestPeriod=600
(2)在applicationContext.xml
中配置资源文件解析器
使用bean标签,配置数据源对象
(3)使用数据源配置SqlSessionFactoryBean
(4)定义数据库接口扫描器,Spring 会扫描数据层接口所在包,同时生成该包下的所有数据层接口的实现类代理对象
(1)扫描业务层的包
(2)配置声明式事务
① 使用bean标签创建事务管理器对象
② 配置通知
③ 配置切面
(1)maven 工程的 pom.xml 文件
4.0.0
com.ssm
SSH_Anno
war
0.0.1-SNAPSHOT
SSH_Anno Maven Webapp
http://maven.apache.org
UTF-8
5.1.6.RELEASE
2.9.8
javax.servlet
javax.servlet-api
3.0.1
provided
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-test
${spring.version}
test
org.mybatis
mybatis
3.4.6
org.mybatis
mybatis-spring
1.3.2
mysql
mysql-connector-java
5.1.6
com.mchange
c3p0
0.9.5.2
org.aspectj
aspectjweaver
1.8.4
log4j
log4j
1.2.17
com.alibaba
fastjson
1.2.47
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.2.2
org.hibernate.validator
hibernate-validator
6.0.0.Final
SSM_Anno
org.apache.maven.plugins
maven-compiler-plugin
1.8
(2)web.xml 文件
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
spring-mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
spring-mvc
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/
HttpMethodFilter
org.springframework.web.filter.HttpPutFormContentFilter
HttpMethodFilter
/*
(3)jbdc.properties 文件
#jdbc\u8FDE\u63A5\u4FE1\u606F
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl = jdbc:mysql://localhost:3306/kunkunstore?useUnicode=true&characterEncoding=utf-8
jdbc.user = root
jdbc.password =admin
#c3p0\u5404\u9879\u5C5E\u6027
c3p0.minPoolSize = 10
c3p0.maxPoolSize = 50
c3p0.initialPoolSize = 20
c3p0.maxIdleTime = 60
c3p0.acquireIncrement = 1
c3p0.checkoutTimeout=6000
c3p0.idleConnectionTestPeriod=600
(4)application.xml 文件
(5)spring.xml 文件
(1)Controller 层的 ProductsController 类
@RestController
@RequestMapping("products")
public class ProductsController {
@Resource
private ProductsService ps;
@GetMapping
public Result select() throws Exception {
return new Result(ps.select(), null, "success", null);
}
@GetMapping("/{pId}")
public Result selectById(@PathVariable("pId")int pId) throws Exception{
return new Result(null,ps.selectById(pId),"success",null);
}
@PostMapping
public Result insert(@Validated Products products,BindingResult br) throws Exception {
if(br.hasErrors()) {
throw new ParamException();
}
ps.insert(products);
return new Result(null,null,"success",null);
}
@PutMapping
public Result updateById(@Validated Products products,BindingResult br) throws Exception {
System.out.println(products);
if(br.hasErrors()) {
throw new ParamException();
}
ps.updateById(products);
return new Result(null,null,"success",null);
}
@DeleteMapping("/{pId}")
public Result deleteById(@PathVariable("pId")int pId) throws Exception {
ps.deleteById(pId);
return new Result(null,null,"success",null);
}
}
(2)Service 层
① Service 接口层 ProductsService 类
public interface ProductsService {
/**
* Description:业务层查询所有数据的方法 </p>
*/
public List select() throws Exception;
/**
* Description:业务层根据id查询数据的方法
*/
public Object selectById(int pId) throws Exception;
/**
* Description: 业务层根据id更新数据的方法
*/
public void updateById(Products products) throws Exception;
/**
* Description: 业务层新增数据的方法
*/
public void insert(Products products) throws Exception;
/**
* Description: 业务层根据id删除数据的方法
*/
public void deleteById(int pId) throws Exception;
}
② Service 实现层 ProductsServiceImp 类
@Service
public class ProductsServiceImp implements ProductsService{
@Resource
private ProductsMapper mapper;
/*
* Description:业务接口层查询所有数据的方法 </p>
*/
@Override
public List select() throws Exception {
return mapper.selectByExample(null);
}
/*
* Description: 业务接口层根据id查询数据的方法</p>
*/
@Override
public Object selectById(int pId) throws Exception {
return mapper.selectByPrimaryKey(pId);
}
/*
*
Description: 业务接口层根据id更新数据的方法</p>
*/
@Override
public void updateById(Products products) throws Exception {
mapper.updateByPrimaryKeySelective(products);
}
/*
*
Description: 业务接口层新增数据的方法</p>
*/
@Override
public void insert(Products products) throws Exception {
mapper.insert(products);
}
/*
*
Description: 业务接口层根据id删除数据的方法</p>
*/
@Override
public void deleteById(int pId) throws Exception {
mapper.deleteByPrimaryKey(pId);
}
}
(3)实体类层
① Products 类
public class Products {
private Integer pId;
@NonNull
@NotEmpty
private String pName;
@NonNull
private Double pPrice;
@NonNull
private Integer pCount;
@NonNull
@NotEmpty
private String pClass;
@NonNull
@NotEmpty
private String pAttribute;
private Integer pTypeid;
//省略set、get方法
}
② Result 类
public class Result {
private List list; //集合封装
private Object object; //对象封装
private String code; //反馈信息
private String message; //显示错误信息
public Result() {}
public Result(List list, Object object, String code, String message) {
this.list = list;
this.object = object;
this.code = code;
this.message = message;
}
//省略set、get方法
}
(4)ProductsMapper 类,定义相关方法
public interface ProductsMapper {
int deleteByPrimaryKey(Integer pId);
int insert(Products record);
int insertSelective(Products record);
List selectByExample(ProductsExample example);
Products selectByPrimaryKey(Integer pId);
int updateByPrimaryKeySelective(Products record);
int updateByPrimaryKey(Products record);
}
(5)统一异常处理层
① 自定义异常类
public class ParamException extends Exception{
}
② ExceptionResolver 类
@Component
public class ExceptionResolver implements HandlerExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception ex) {
FastJsonJsonView view = new FastJsonJsonView();
ex.printStackTrace();
Map map = new HashMap();
map.put("code", "error");
if(ex instanceof ParamException) {
map.put("message", "参数错误");
}else {
map.put("message", "服务器异常");
}
view.setAttributesMap(map);
ModelAndView mav = new ModelAndView();
mav.setView(view);
return mav;
}
}
Insert title here
查询所有
编号
名称
价格
库存
分类
属性