我们先准备好IDEA
首先我们先来看一下我们所要创建的各个包名吧
1、DAO层: DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此。
2、Service层: Service层 主要负责业务模块
的逻辑应用设计。同样是首先设计接口,再设计其实现的类,接着再Spring的配置文件中配置其实现的关联。
3、Controller层:Controller层负责具体的业务模块流程的控制,在此层里面要调用Serice层的接口来控制业务流程,控制的配置也同样是在Spring的配置文件里面进行,针对具体的业务流程。
4、View层此层与控制层结合比较紧密,需要二者结合起来协同工发。View层 主要负责前台jsp页面的表示。
Controller层里面的FlowerController如下
package cn.kj.controller;
import cn.kj.Service.FlowerService;
import cn.kj.Service.FlowerServicelmpl;
import cn.kj.pojo.Flower;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class FlowerController {
@Autowired
private FlowerServicelmpl fl;
@RequestMapping("/getAll")
@ResponseBody
public List getAll() {
return fl.getAll();
}
//先根据id查询出来再修改
@RequestMapping ("/getInfo")
@ResponseBody
public ModelAndView getInfo(@RequestParam("id") int id) {
ModelAndView mv = new ModelAndView();
mv.addObject("info", fl.getinfo(id));
mv.setViewName("jsp/update");
return mv;
}
//新增
@RequestMapping ("/add")
@ResponseBody
public ModelAndView add (Flower flower){
ModelAndView model=new ModelAndView();
int i=fl.add(flower);
model.setViewName("index");
return model;
}
//删除
@RequestMapping ("/delete")
@ResponseBody
public ModelAndView del ( @Param("id") int id){
ModelAndView model=new ModelAndView();
fl.delete(id);
model.setViewName("index");
return model;
}
//修改
@RequestMapping ("/update")
@ResponseBody
public ModelAndView update (Flower flower){
ModelAndView model=new ModelAndView();
fl.update(flower);
model.setViewName("index");
return model;
}
}
Dao层里面的FlowerMapper、FlowerMapper.xml如下
package cn.kj.dao;
import cn.kj.pojo.Flower;
import java.util.List;
public interface FlowerMapper {
List getAll();//查询
int add(Flower flower);//新增
int delete(int id);//删除
Flower getinfo(int id);//根据id修改
int update(Flower flower);//修改
}
//查询的sql语句
//删除的sql语句
delete from flower where id = #{id}
//新增的sql语句
INSERT INTO `flower`.`flower`( `name`, `anothername`, `property`, `price`, `production`) VALUES
(#{name}, #{anothername}, #{property}, #{price}, #{production});
//修改的sql语句
UPDATE `flower` SET `name` = #{name}, `anothername` = #{anothername}, `property` = #{property}, `price` =#{price}, `production` = #{production} WHERE `id` = #{id};
//根据id查询
实体类 Flower如下
然后用get、set封装
package cn.kj.pojo;
public class Flower {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAnothername() {
return anothername;
}
public void setAnothername(String anothername) {
this.anothername = anothername;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
private String name;
private String anothername;
private String property;
private String production;
public String getProduction() {
return production;
}
public void setProduction(String production) {
this.production = production;
}
private int price;
}
Service层里面FlowerService如下
和Dao层里面的FlowerMapper一样的
package cn.kj.Service;
import cn.kj.pojo.Flower;
import java.util.List;
public interface FlowerService {
List getAll();
int add(Flower flower);
int delete(int id);
Flower getinfo(int id);
int update(Flower flower);
}
Service层里面FlowerServicelmpl如下
implements实现FlowerServicelmpl
package cn.kj.Service;
import cn.kj.dao.FlowerMapper;
import cn.kj.pojo.Flower;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class FlowerServicelmpl implements FlowerService {
@Autowired
private FlowerMapper Flower;
@Override
public List getAll() {
return Flower.getAll();
}
@Override
public int add(Flower flower) {
return Flower.add(flower);
}
@Override
public int delete(int id) {
return Flower.delete(id);
}
@Override
public Flower getinfo(int id) {
return Flower.getinfo(id);
}
@Override
public int update(Flower flower) {
return Flower.update(flower);
}
}
database.properties 数据库连接的配置
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/flower?useUnicode=true&characterEncoding=utf-8
username=root//连接数据库的用户名
password=caikaijin//连接数据库的密码
index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
主页查询
XX信息列表
新增
//下面的名字随便取
编号
名称
名字
随便
价格
地方
操作
add.jsp新增页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
新增信息
update.jsp修改的页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
修改信息
最后两步
一、要写好web.xml
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
characterEncodingFilter
/*
default
*.js
*.css
/assets/*"
/images/*
contextConfigLocation
classpath*:spring.xml
org.springframework.web.context.ContextLoaderListener
SSM4
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:springmvc.xml
1
SSM4
/
最后一步,也是最重要的一步
要把这些复制粘贴到pom.xml然后等待,等到不bug的时候就可以了
4.0.0
SSM4
SSM4
1.0-SNAPSHOT
war
SSM4 Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
4.3.13.RELEASE
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jdbc
${spring.version}
javax.servlet
javax.servlet-api
3.1.0
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
javax.servlet
jstl
1.2
log4j
log4j
1.2.17
org.slf4j
slf4j-api
1.7.25
org.slf4j
slf4j-log4j12
1.7.25
test
org.slf4j
slf4j-simple
1.7.25
test
org.mybatis
mybatis
3.4.2
org.mybatis
mybatis-spring
1.3.0
mysql
mysql-connector-java
5.1.29
com.fasterxml.jackson.core
jackson-databind
2.8.9
commons-dbcp
commons-dbcp
1.4
commons-pool
commons-pool
1.5.4
com.alibaba
fastjson
1.2.38
com.github.pagehelper
pagehelper
5.0.0
src/main/java
**/*.xml
SSM4
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2