Spring+SpringMVC+Mybatis
Spring负责bean配置和事务,aop等
SpringMVC负责web层逻辑及JSP页面
Mybatis负责持久层逻辑,与数据库进行交互
Mybatis层:
高度集成,降低耦合,Mybatis配置文件存在的必要性越来越小,在Mybatis配置中配置些次重要的配置(别名等等)证明Mybatis确实存在
typeAliases>
pojo实体类,尽量与数据库中数据字段一致
@Data@AllArgsConstructor@NoArgsConstructorpublic class User {
private String name; private String pass; private int age;}
编写dao层接口
public interface UserMapper {
//查询全部User ListqueryAll(); //查询某一User User queryOne(String name); //增加一个User int addOne(User user); //删除一个User int delOne(String name); //修改一个User int updateOne(User user);}
编写dao层接口的配置文件CRUD
namespace=
编写service层接口,和dao层接口一致,service接受请求后调用dao层
编写service层接口的实现类
package com.bj.service;import com.bj.dao.UserMapper;import com.bj.pojo.User;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import java.util.List;//@Transactional@Servicepublic class UserServiceImpl implements UserService{
@Autowired private UserMapper userMapper; public ListqueryAll() {
return userMapper.queryAll(); } public User queryOne(String name) {
return userMapper.queryOne(name); } public int addOne(User user) {
return userMapper.addOne(user); } public int delOne(String name) {
return userMapper.delOne(name); } public int updateOne(User user) {
return userMapper.updateOne(user); }}
Spring配置文件
bean> bean> bean>
配置Spring配置中应用的jdbc.properties属性文件
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/happusername=rootpassword=1452
配置applicationContext配置,用来集成Spring和Mybatis
beans>
此时可以编写测试类来测试程序是否能跑起来,在此之前先实现dao层接口实现类
package com.bj.service;import com.bj.dao.UserMapper;import com.bj.pojo.User;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;import java.util.List;@Componentpublic class UserMapperImpl implements UserMapper{
@Autowired @Qualifier(value = "sqlSession") private SqlSessionTemplate sqlSession; public ListqueryAll() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class); List list = mapper.queryAll(); return list; }}
篇幅原因就只写一个查询全部用户信息的方法
@Test public void queryAll(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring-Dao.xml"); UserMapper userMapperImpl = context.getBean("userMapperImpl", UserMapper.class); Listlist = userMapperImpl.queryAll(); for (User user : list) {
System.out.println(user); } }
测试类加@Test注解
执行结果:
注意,jdbc.properties的编码格式若是gbk,要改为utf-8,否则乱码导致数据库连接失败
SpringMVC层:
在web.xml中配置dispatcherservlet中央处理器及过滤器filter,filter在servlet执行前执行,将字符转为utf-8格式,防止乱码
springmvcservlet-name> org.springframework.web.servlet.DispatcherServletservlet-class> contextConfigLocationparam-name> classpath:applicationContext.xmlparam-value> init-param> 1load-on-startup> servlet> springmvcservlet-name> /url-pattern> servlet-mapping> encodingfilter-name> org.springframework.web.filter.CharacterEncodingFilterfilter-class> encodingparam-name> utf-8param-value> init-param> filter> encodingfilter-name> /*url-pattern> filter-mapping>
SpringMVC配置,开启包扫描,注解驱动和视图解析器,并配置前缀后缀
bean>
此时WEB-INF下应有对应的文件夹
配置服务层配置,开启包扫描
保证spring-Dao.xml,service-config.xml,springmvc-servlet.xml在同一目录下
在applicationContext.xml中加入
接下来编写controller程序
package com.bj.controller;import com.bj.pojo.User;import com.bj.service.UserService;import com.sun.org.apache.xpath.internal.operations.Mod;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 java.util.List;@Controllerpublic class UserController {
@Autowired private UserService userService; @RequestMapping("/queryAll") public String queryAll(Model model){
List list = userService.queryAll(); model.addAttribute("list", list); return "queryAll"; } @RequestMapping("/queryOne") public String queryOne(Model model,String name){
User user = userService.queryOne(name); model.addAttribute("user",user); return "queryOne"; } @RequestMapping("/addOne") public String addOne(User user){
int i = userService.addOne(user); if(i>0){
return "success"; } else{
return "fail"; } } @RequestMapping("/delOne") public String delOne(String name){
int i = userService.delOne(name); if(i>0){
return "success"; } else{
return "fail"; } }}
编写jsp文件,这里只展示增加用户和查询用户
主页:
查询全部用户a>
查询某用户a>
增加用户a>
删除用户a>
增加页:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> Titletitle> head> 添加用户 h1> div> div> div>