SSM框架集成

SSM

主流的项目架构的三大框架

 SpringMVC: Web层框架,spring的一个模块

 Spring:容器框架

 MyBatis:持久层框架


准备相关jar包

spring依赖包


 SpringMVC依赖包


Mybatis依赖包


Mybatis支持的日志包log4j 


Mybatis和Spring框架集成的桥梁包


数据库驱动包和连接池


 Jstl标签库依赖包


各种配置文件





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">


"cn.zj.ssm.mapper.UserMapper">

    "insert"parameterType="cn.zj.ssm.pojo.User">

        insert into user  (name,password,age)values(#{name},#{password},#{age})

   


    "selectByPrimaryKey"parameterType="Integer"resultType="cn.zj.ssm.pojo.User">

        select * from user  where id = #{id}

   


    "selectList"  resultType="cn.zj.ssm.pojo.User">

        select * from user

   


    "delteByPrimaryKey"parameterType="int">

        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 selectList() {

        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"?>

"http://www.springframework.org/schema/beans"

    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">

"cn.zj.mybatis"/>



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"?>

"http://www.springframework.org/schema/beans"

    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

        ">



   

    "classpath:db.properties"/>


   

    "dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"destroy-method="close">

        "driverClassName"value="${jdbc.driverClassName}"/>

        "url"value="${jdbc.url}"/>

        "username"value="${jdbc.username}"/>

        "password"value="${jdbc.password}"/>

        "maxActive"value="${jdbc.maxActive}"/>

   



   

    "sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

       

        "dataSource"ref="dataSource"/>



       

        "mapperLocations">

           


               

                classpath:cn/zj/mybatis/mapper/*Mapper.xml

           

       


       

        "typeAliasesPackage"value="cn.zj.mybatis.pojo"/>


       

         "configLocation"value="classpath:mybatis-config.xml"/>

   

创建MyBatis的Mapper接口的代理对象

使用桥梁包org.mybatis.spring.mapper.MapperFactoryBean 创建UserMapper代理对象



此种方式每一个Mapper接口需要单独配置,如果Mapper过多,创建Mapper可能造成配置代码过多

     


      "userMapper"class="org.mybatis.spring.mapper.MapperFactoryBean">


          

           "sqlSessionFactory"ref="sqlSessionFactory"/>


          

           "mapperInterface"value="cn.zj.mybatis.mapper.UserMapper"/>


     



使用包扫描创建MyBatis的Mapper接口的代理对象




  "org.mybatis.spring.mapper.MapperScannerConfigurer">

       

        "basePackage"value="cn.zj.mybatis.mapper"/>


       

        "sqlSessionFactoryBeanName"value="sqlSessionFactory"/>


 


MyBatis的事务管理器的配置

一般开发,事务的管理都会使用aop切入到业务层


     

      "transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       

        "dataSource"ref="dataSource"/>

     


     

      "txAdvice"transaction-manager="transactionManager">


       

       

           

            "get*"read-only="true"isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

            "select*"read-only="true"isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

            "find*"read-only="true"isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

            "query*"read-only="true"isolation="REPEATABLE_READ"  propagation="REQUIRED"/>


           

            "*"read-only="false"isolation="REPEATABLE_READ"  propagation="REQUIRED"/>


       


     


     


     

       

        "execution(*

  cn.zj.mybatis.service..*.*(..))"id="pt"/>


       

        "txAdvice"pointcut-ref="pt"/>

     



SpringMVC的集成

在web.xml配置SpringMVC的前端控制器

"1.0"encoding="UTF-8"?>

"http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0">


   

   

       CharacterEncodingFilter

        org.springframework.web.filter.CharacterEncodingFilter

      

           encoding

           UTF-8

      

   

   

       CharacterEncodingFilter

       /*

   



   

   

       MVC

        org.springframework.web.servlet.DispatcherServlet

      

           contextConfigLocation


          


           classpath:spring.xml

      


       1


   

   

       MVC

       *.do

   


[if !supportLists]2.1.1.  [endif]springmvc.xml配置文件

"1.0"encoding="UTF-8"?>

"http://www.springframework.org/schema/beans"

    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"%>

"UTF-8">

Insert title here

   

用户列表


    "1"style="width: 500px;" cellspacing="0">

       

            id

            名称

            密码

            年龄

            操作

       



        "${users}"var="user">

           

                ${user.id}

                ${user.name}

                ${user.password}

                ${user.age}

               

                    "javascript:void(0);"onclick="deleteByPrimaryKey(${user.id})">删除 

                    "">修改

               

           

       

   


"text/javascript">


    function deleteByPrimaryKey(userId){

        if(confirm("亲,您确定删除此条数据么?")){

                window.location.href="${pageContext.request.contextPath}/user/delete.do?id="+userId;

        }

    }

你可能感兴趣的:(SSM框架集成)