搭建整合环境
整合说明
整合方式有很多,我选择xml+注解的方式,单个maven工程(不使用maven父子工程)
整合思路
- 先搭建整合的环境
- 先把spring的配置搭建完成
- 在使用spring框架整合SpringMVC
- 最后使用Spring整合Mybatis
项目结构
首先引入依赖
4.0.0
org.example
ssm
1.0-SNAPSHOT
war
UTF-8
1.8
1.8
5.0.2.RELEASE
1.6.6
1.2.17
8.0.12
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.13
compile
mysql
mysql-connector-java
${mysql.version}
runtime
javax.servlet
servlet-api
2.5
provided
javax.servlet.jsp
jsp-api
2.2
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
c3p0
c3p0
0.9.1.2
jar
compile
ssm
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.8
UTF-8
true
编写实体类,及dao层
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 +
'}';
}
}
@Repository("accountDao")
public interface AccountDao {
@Select("select *from account")
List findAll();
@Insert("insert into account(name,money) values(#{name},#{money})")
void saveAccount(Account account);
}
编写service层代码
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
@Qualifier("accountDao")
private AccountDao accountDao;
@Override
public List findAll() {
System.out.println("业务层,查询所有");
return accountDao.findAll();
}
@Override
public void saveAccount(Account account) {
System.out.println("业务层,保存账户");
accountDao.saveAccount(account);
}
}
编写controller层代码
@Controller
@RequestMapping("/account")
public class AccountController{
@Autowired
private AccountService accountService;
@RequestMapping("/findAll")
public String findAll(Model model){
System.out.println("表现层,查询所有账户。。。。。");
List accounts = accountService.findAll();
model.addAttribute("accounts",accounts);
//调用业务层的方法
return "list";
}
@RequestMapping("/save")
public void saveAccount(Account account, HttpServletRequest request,HttpServletResponse response) throws IOException {
System.out.println("表现层,保存账户。。。。。");
accountService.saveAccount(account);
//调用业务层的方法
response.sendRedirect(request.getContextPath()+"/account/findAll");
}
}
编写spring和mybatis的配置文件(合到一起)
编写SpringMVC的配置文件及Web.xml
web.xml:
Archetype Created Web Application
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
编写index.js , list.jsp ,success.jsp
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
Title
测试查询所有用户
测试保存
list.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
查询所有账户成功
${account.name}
success.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
成功页面