(1)dao、service都由Spring容器构架(applicationContext.xml)
(2)controller由SpringMVC容器构建(springmvc-servlet.xml)
1.配置pom依赖
4.0.0
org.example
1012_2_ssm
1.0-SNAPSHOT
war
4.3.5.RELEASE
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-aspects
${spring.version}
aopalliance
aopalliance
1.0
aopalliance
aopalliance
1.0
org.aspectj
aspectjweaver
1.9.2
aopalliance
aopalliance
1.0
org.mybatis
mybatis
3.2.7
mysql
mysql-connector-java
5.1.47
com.alibaba
druid
1.1.10
org.mybatis
mybatis-spring
1.3.2
com.fasterxml.jackson.core
jackson-databind
2.9.8
org.projectlombok
lombok
1.18.6
org.springframework
spring-test
${spring.version}
junit
junit
4.12
test
2.web.xml:配置Spring+filter乱码+SpringMVC
web.xml为自己创建:
contextConfigLocation
classpath:application.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc-servlet.xml
springmvc
/
3.创建实体entity层+mapper层(dao)
package entity;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
/**
* zt
* 2020/10/12
* 15:19
*/
@Data
public class Product {
private int pid;
private int tid;
private String pname;
private Date ptime;
private String pinfo;
private BigDecimal pprice;
private int pstate;
private String pimage;
}
package mapper;
import entity.Product;
import java.util.List;
/**
* zt
* 2020/10/12
* 15:22
*/
public interface ProductMapper {
public ListfindAll();
public Product findById(int id);
public int update(Product product);
public int add(Product product);
}
4.为mapper层(dao)创建对应的mapper文件(里面写增删改查sql语句)
p_id as pid,t_id as tid,p_name as pname,p_time as ptime,p_info as pinfo,p_price as pprice,
p_state as pstate,p_image as pimage
select
from product
select
from product where p_id = #{id}
update product
p_name=#{pname},
p_info=#{pinfo},
p_price=#{pprice},
where p_id = #{pid}
insert into product(t_id,p_name,p_info,p_price,p_image,p_state,p_time)
value(#{tid},#{pname},#{pinfo},#{pprice},#{pimage},#{pstate},#{ptime})
5.service层
其中ProductServiceImpl引入dao,使用注解@Resource注入,将dao从spring容器中引入
package service;
import entity.Product;
import java.util.List;
/**
* zt
* 2020/10/12
* 15:35
*/
public interface ProductService {
public ListfindAll();
public Product findByid(int id);
public int update(Product product);
public int add(Product product);
}
package service.impl;
import entity.Product;
import mapper.ProductMapper;
import org.springframework.stereotype.Service;
import service.ProductService;
import javax.annotation.Resource;
import java.util.List;
/**
* zt
* 2020/10/12
* 15:36
*/
@Service("productServiceImpl")
public class ProductServiceImpl implements ProductService {
@Resource
private ProductMapper productMapper;
@Override
public ListfindAll() {
return productMapper.findAll();
}
@Override
public Product findByid(int id) {
return productMapper.findById(id);
}
@Override
public int update(Product product) {
return productMapper.update(product);
}
@Override
public int add(Product product) {
return productMapper.add(product);
}
}
6.创建controller
自动注入ProductService
package controller;
import entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.ProductService;
import utils.R;
import java.util.*;
/**
* zt
* 2020/10/12
* 15:39
*/
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
@RequestMapping("/findAll")
@ResponseBody
public ListfindAll(){
return productService.findAll();
}
@RequestMapping("/add")
@ResponseBody
public R add(@RequestBody Product product){
product.setTid(1);
product.setPtime(new Date());
// System.out.println(product.toString());
int i = productService.add(product);
if (i>0){
return R.ok("新增成功");
}
return R.error("新增失败");
}
}
7.spring的配置文件applicationContext.xml
1.加载properties
2.配置SqlSessionFactory
3.构建mapper
4.构建service(包扫描)
数据库配置dbinfo.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///myshop?characterEncoding=utf-8
#不要直接命名username 不然冲突了
jdbc.username=root
#m默认spring内部已经存在username=你电脑的主机名的一个键值对
jdbc.password=root
8.SpringMVC配置springmvc-servlet.xml
包扫描+注解驱动+静态资源放行
9.页面
index.html
Title
新增
add.html
add
新增页面
商品名称
价格
商品信息
新增
10.运行结果