dbinfo.properties mysql_完美起航-SSM整合:商城小项目001(配置SSM环境+查询所有操作+添加操作)...

(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为自己创建:

e9f0f20d553c48deaf14710b7a79bb10.png

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)

87a9b591f0730adafa4f8528ee74750b.png

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语句)

c001e1bac87fb3ab93db7c725bd9cdf0.png

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容器中引入

43ed643d2684a006fb3baef1f55ae4de.png

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

3561c51155a33b63f39d30543af3c271.png

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

b105fcaad9331c7ab82df020df97767a.png

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.页面

a4196ca07840d774354da2a3dab14dd9.png

index.html

Title

新增

add.html

add

新增页面

商品名称

价格

商品信息

新增

10.运行结果

b1cfefdd5690ed1bb040eb0a3e3aebcf.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值