create table account(
id int primary key auto_increment,
name varchar(20),
money double
);
4.创建mavenjavaweb工程,引入相关依赖
5.0.2.RELEASE
1.6.6
1.2.12
5.1.6
3.4.5
org.aspectj
aspectjweaver
1.6.8
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
junit
junit
4.12
mysql
mysql-connector-java
${mysql.version}
javax.servlet
servlet-api
2.5
provided
javax.servlet.jsp
jsp-api
2.0
provided
jstl
jstl
1.2
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.0
com.alibaba
druid
1.1.10
ssm
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.8
UTF-8
true
5.编写实体类,在ssm_model项目中编写
package com.qcby.model;
import java.io.Serializable;
/**
* 账户类
*/
public class Account implements Serializable {
// 主键
private Integer id;
// 账户名称
private String name;
// 账号的金额
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
1. 在ssm_web项目中创建applicationContext.xml的配置文件,编写具体的配置信息。
2、创建service包,编写service接口和实现类
package com.qcby.service;
import com.qcby.model.Account;
import java.util.List;
/**
* 账户业务层接口
*/
public interface AccountService {
// 查询所有
public List findAll();
//增添账户信息
public int insert(Account account);
//修改账户信息
public int update(Account account);
//删除账户信息
public int delete(Integer id);
}
package com.qcby.service.impl;
import com.qcby.mapper.AccountMapper;
import com.qcby.model.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 业务层实现类
* ssm整合业务层接口
*/
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
/**
* 查询所有账户信息
* @return
*/
@Override
public List findAll() {
System.out.println("业务层执行了!!!");
List accountList = accountMapper.findAll();
return accountList;
}
@Override
public int insert(Account account) {
System.out.println("新增业务层执行了!!!");
int i = accountMapper.insert(account);
return i;
}
@Override
public int update(Account account) {
System.out.println("修改业务层执行了!!!");
int i = accountMapper.update(account);
return i;
}
@Override
public int delete(Integer id) {
System.out.println("删除业务层执行了!!!");
int i = accountMapper.delete(id);
return i;
}
}
contextConfigLocation
classpath:applicationContext.xml
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
org.springframework.web.context.ContextLoaderListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
dispatcherServlet
/
2.创建springmvc.xml的配置文件,编写配置文件
3.测试SpringMVC的框架搭建是否成功
编写index.jsp,超链接和表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页
查询所有账户信息
查询所有
添加账户信息
修改账户信息
删除账户信息
res.jsp页面编写,结果返回页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
整合成功
SSM整合成功页面!!!!
序号
姓名
工资
${account.id}
${account.name}
${account.money}
${result}
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
3.编写AccountController,在controller中注入service对象,调用service对象的方法进行测试
package com.qcby.controller;
import com.qcby.model.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
// 注入
@Autowired
private AccountService accountService;
/**
* 查询所有
* @return
*/
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
System.out.println("表现层:查询所有的账号...");
// 调用service对象的方法
List list = accountService.findAll();
ModelAndView mv = new ModelAndView();
mv.setViewName("res");
return mv;
}
}
insert into account(name,money) values(#{name},#{money})
update account set name = #{name},money=#{money} where id = #{id}
delete from account where id = #{id}
2.编写AccountMapper接口
package com.qcby.mapper;
import com.qcby.model.Account;
import java.util.List;
/**
* ssm整合持久层接口
* 实现查询所有的账户信息
*/
public interface AccountMapper {
// 查询所有
public List findAll();
//新增
public int insert(Account account);
//更新
public int update(Account account);
//删除
public int delete(Integer id);
}
当Tomcat启动后,会加载web.xml文件,并根据其配置加载相关的配置文件
项目目录: