SSM
主流的项目架构的三大框架
SpringMVC: Web层框架,spring的一个模块
Spring:容器框架
MyBatis:持久层框架
准备相关jar包
Mapper层
public interface UserMapper {
int insert(User user);
User selectByPrimaryKey(Integerid);
List selectList();
int delteByPrimaryKey(Integer id);
}
Mapperxml文件
"1.0"encoding="UTF-8"?>
PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into user (name,password,age)values(#{name},#{password},#{age})
select * from user where id = #{id}
select * from user
delete from user where id = #{id}
Service层 完成项目层与层之间spring对象的创建和依赖关系的维护
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper mapper;
public int insert(User user) {
return mapper.insert(user);
}
public User selectByPrimaryKey(Integer id) {
System.out.println(mapper);
return mapper.selectByPrimaryKey(id);
}
public List
return mapper.selectList();
}
public int delteByPrimaryKey(Integer id) {
return mapper.delteByPrimaryKey(id);
}
}
测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class UserServiceTest {
@Autowired
private UserService service;
@Test
public void testInsert() {
Useruser = new User(null, "乔峰", "qiaofeng", 30);
int row = service.insert(user);
}
@Test
public void testSelectByPrimaryKey() {
Useruser = service.selectByPrimaryKey(8);
}
@Test
public void testSelectList() throws Exception {
Listusers = service.selectList();
for (User user : users) {
System.out.println(user);
}
}
}
applicationContext配置文件的配置
"1.0"encoding="UTF-8"?>
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
MyBatis 框架SqlSessionFactory对象的创建
UserMapper 代理对象使用 SqlSession 操作对象创建
SqlSessionFactory工厂对象创建SqlSession 对象
SqlSessionFactory对象如何创建
1,和Spring框架集成之前 MyBatis框架自己读取配置文件中的相关配置去创建
2,和Spring框架集成之后交给Spring容器来创建
在 mybatis和spring集成的类查阅桥梁包
org.mybatis.spring.SqlSessionFactoryBean创建 SqlSessionFactory
配置文件
"1.0"encoding="UTF-8"?>
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
创建MyBatis的Mapper接口的代理对象
使用桥梁包org.mybatis.spring.mapper.MapperFactoryBean
此种方式每一个Mapper接口需要单独配置,如果Mapper过多,创建Mapper可能造成配置代码过多
使用包扫描创建MyBatis的Mapper接口的代理对象
MyBatis的事务管理器的配置
一般开发,事务的管理都会使用aop切入到业务层
cn.zj.mybatis.service..*.*(..))"id="pt"/>
SpringMVC的集成
在web.xml配置SpringMVC的前端控制器
"1.0"encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0">
[if !supportLists]2.1.1. [endif]springmvc.xml配置文件
"1.0"encoding="UTF-8"?>
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
编写控制器 UserController
@Controller
@RequestMapping("/user")
public class UserController
{
@Autowired
private UserService service;
@RequestMapping("/list")
public String list(Model m) {
//调用service查询所有用户方法
Listusers = service.selectList();
//共享数据
m.addAttribute("users", users);
return "/WEB-INF/view/user_list.jsp";
}
@RequestMapping("/delete")
public String delete(Integer id) {
System.out.println(id);
//调用service层的删除方法
service.delteByPrimaryKey(id);
return "redirect:/user/list.do";
}
}
user_list.jsp 页面
在jsp页面使用jstl标签库需要先在页面引入jstl 标签库
<%@pagelanguage="java"contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>
用户列表
function deleteByPrimaryKey(userId){
if(confirm("亲,您确定删除此条数据么?")){
window.location.href="${pageContext.request.contextPath}/user/delete.do?id="+userId;
}
}