zoukankan      html  css  js  c++  java
  • Spring SpringMVC Mybatis整合

    spring 配置文件:beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">
        <!-- 扫描服务包 -->
        <context:component-scan base-package="com.ssm.service" />
        <!-- 数据源 -->
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
            <property name="username" value="root" />
            <property name="password" value="root" />
        </bean>
        <!-- sqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation" value="classpath:sqlMapConfig.xml" />
            <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml" />
        </bean>
        <!-- dao -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.ssm.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        </bean>
    
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager" />
        <context:annotation-config />
    
        <!-- 扫描服务包 -->
        <context:component-scan base-package="com.ssm2.service" />
        <!-- 数据源 -->
        <bean id="dataSource2"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property>
            <property name="username" value="root" />
            <property name="password" value="root" />
        </bean>
        <!-- sqlSessionFactory -->
        <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource2" />
            <property name="configLocation" value="classpath:sqlMapConfig2.xml" />
            <property name="mapperLocations" value="classpath:com/ssm2/mapper/*.xml" />
        </bean>
        <!-- dao -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.ssm2.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" />
        </bean>
    
        <bean id="transactionManager2"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource2" />
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager2" />
        
    </beans>

    springMVC配置文件:springmvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">
        <!-- spring mvc 注解驱动 -->
        <mvc:annotation-driven />
        <!-- 扫描器 -->
        <context:component-scan base-package="com.ssm.controller" />
        <!-- 配置视图 解析器 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <!-- 前缀和后缀 -->
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <!-- 文件上传配置 -->
        <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize" value="50000000" />
        </bean>
        <!-- 从请求和响应读取/编写字符串 -->
        <bean id="stringHttpMessage"
            class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    
        <!-- 用于将对象转换为JSON -->
        <bean id="jsonConverter"
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
    
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="stringHttpMessage" />
                    <ref bean="jsonConverter" />
                </list>
            </property>
        </bean>
    
    
    </beans>

    myBatis配置文件:sqlMapConfig.xml, sqlMapConfig2.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <!--设置日志输出-->
        <settings>
            <setting name="logImpl" value="LOG4J" />
        </settings>
        <typeAliases>
            <typeAlias type="com.ssm.model.Person" alias="Person" />
        </typeAliases>
        
    </configuration>
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        <typeAliases>
            <typeAlias type="com.ssm2.model.Person2" alias="Person2" />
        </typeAliases>
    
    </configuration>

     log4j.properties

    log4j.rootLogger=DEBUG, Console
    #Console
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.layout=org.apache.log4j.PatternLayout
    log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
    log4j.logger.java.sql.ResultSet=INFO
    log4j.logger.org.apache=INFO
    log4j.logger.java.sql.Connection=DEBUG
    log4j.logger.java.sql.Statement=DEBUG
    log4j.logger.java.sql.PreparedStatement=DEBUG
    #设置日志输出...
    log4j.logger.com.ssm.dao=DEBUG

     日志输出参考:

    http://mcj8089.iteye.com/blog/2038891

    http://blog.csdn.net/isea533/article/details/22931341

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="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"
        id="WebApp_ID" version="3.0">
        <!-- spring 监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- spring 启动文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:beans.xml</param-value>
        </context-param>
        <!-- spring mvc 配置 -->
        <servlet>
            <servlet-name>springMVC</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springMVC</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    
        <!-- 解决工程编码过滤器 -->
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>*.do</url-pattern>
        </filter-mapping>
    
    </web-app>

    model java文件:

    package com.ssm.model;
    
    import java.util.Date;
    
    public class Person {
        private Integer personId;
    
        private String name;
    
        private Integer gender;
    
        private String personAddr;
    
        private Date birthday;
    
        public Integer getPersonId() {
            return personId;
        }
    
        public void setPersonId(Integer personId) {
            this.personId = personId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getGender() {
            return gender;
        }
    
        public void setGender(Integer gender) {
            this.gender = gender;
        }
    
        public String getPersonAddr() {
            return personAddr;
        }
    
        public void setPersonAddr(String personAddr) {
            this.personAddr = personAddr;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        @Override
        public String toString() {
            return "Person [personId=" + personId + ", name=" + name + ", gender="
                    + gender + ", personAddr=" + personAddr + ", birthday="
                    + birthday + "]";
        }
        
    }

    mapper xml文件:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.ssm.dao.PersonMapper">
        <resultMap id="BaseResultMap" type="com.ssm.model.Person">
            <id column="person_id" property="personId" jdbcType="INTEGER" />
            <result column="name" property="name" jdbcType="VARCHAR" />
            <result column="gender" property="gender" jdbcType="INTEGER" />
            <result column="person_addr" property="personAddr" jdbcType="VARCHAR" />
            <result column="birthday" property="birthday" jdbcType="DATE" />
        </resultMap>
        <sql id="Base_Column_List">
            person_id, name, gender, person_addr, birthday
        </sql>
        <select id="selectByPrimaryKey" resultMap="BaseResultMap"
            parameterType="java.lang.Integer">
            select
            <include refid="Base_Column_List" />
            from person
            where person_id = #{personId,jdbcType=INTEGER}
        </select>
        <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
            delete from person
            where person_id = #{personId,jdbcType=INTEGER}
        </delete>
        <insert id="insert" parameterType="com.ssm.model.Person">
            insert into person (person_id,
            name, gender,
            person_addr, birthday)
            values
            (#{personId,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
            #{gender,jdbcType=INTEGER},
            #{personAddr,jdbcType=VARCHAR},
            #{birthday,jdbcType=DATE})
        </insert>
        <insert id="insertSelective" parameterType="com.ssm.model.Person">
            insert into person
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="personId != null">
                    person_id,
                </if>
                <if test="name != null">
                    name,
                </if>
                <if test="gender != null">
                    gender,
                </if>
                <if test="personAddr != null">
                    person_addr,
                </if>
                <if test="birthday != null">
                    birthday,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="personId != null">
                    #{personId,jdbcType=INTEGER},
                </if>
                <if test="name != null">
                    #{name,jdbcType=VARCHAR},
                </if>
                <if test="gender != null">
                    #{gender,jdbcType=INTEGER},
                </if>
                <if test="personAddr != null">
                    #{personAddr,jdbcType=VARCHAR},
                </if>
                <if test="birthday != null">
                    #{birthday,jdbcType=DATE},
                </if>
            </trim>
        </insert>
        <update id="updateByPrimaryKeySelective" parameterType="com.ssm.model.Person">
            update person
            <set>
                <if test="name != null">
                    name = #{name,jdbcType=VARCHAR},
                </if>
                <if test="gender != null">
                    gender = #{gender,jdbcType=INTEGER},
                </if>
                <if test="personAddr != null">
                    person_addr = #{personAddr,jdbcType=VARCHAR},
                </if>
                <if test="birthday != null">
                    birthday = #{birthday,jdbcType=DATE},
                </if>
            </set>
            where person_id = #{personId,jdbcType=INTEGER}
        </update>
        <update id="updateByPrimaryKey" parameterType="com.ssm.model.Person">
            update person
            set
            name = #{name,jdbcType=VARCHAR},
            gender = #{gender,jdbcType=INTEGER},
            person_addr = #{personAddr,jdbcType=VARCHAR},
            birthday =
            #{birthday,jdbcType=DATE}
            where person_id =
            #{personId,jdbcType=INTEGER}
        </update>
    </mapper>

    dao java接口文件:

    package com.ssm.dao;
    
    import com.ssm.model.Person;
    
    public interface PersonMapper {
    
        int deleteByPrimaryKey(Integer personId);
    
        int insert(Person record);
    
        int insertSelective(Person record);
    
        Person selectByPrimaryKey(Integer personId);
    
        int updateByPrimaryKeySelective(Person record);
    
        int updateByPrimaryKey(Person record);
    }

    service java文件:

    package com.ssm.service;
    
    import com.ssm.model.Person;
    
    public interface PersonServiceI {
        
        public Person getPersonById(Integer personId);
    }
    package com.ssm.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.ssm.dao.PersonMapper;
    import com.ssm.model.Person;
    
    @Service("personService")
    public class PersonServiceImp implements PersonServiceI {
    
        @Autowired
        private PersonMapper personMapper;
    
        
        @Override
        public Person getPersonById(Integer personId) {
            return personMapper.selectByPrimaryKey(personId);
        }
    
    }

    controller java文件:

    package com.ssm.controller;
    
    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 com.ssm.service.PersonServiceI;
    
    @Controller
    @RequestMapping("/person")
    public class PersonController {
    
        @Autowired
        PersonServiceI personService222;
    
        @RequestMapping("/showPerson")
        public String showUser(Model model) {
            model.addAttribute("person", personService222.getPersonById(1));
            return "showPerson";
        }
    }

    WEB-INF/lib/下面的jar文件:

    bean-validator.jar
    com.springsource.net.sf.cglib-2.2.0.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    commons-fileupload-1.3.1.jar
    commons-io-2.2.jar
    commons-logging-1.1.3.jar
    jackson-annotations-2.5.0.jar
    jackson-core-2.5.0.jar
    jackson-databind-2.5.0.jar
    log4j-1.2.17.jar
    log4j-api-2.0.2.jar
    log4j-core-2.0.2.jar
    mybatis-3.2.8.jar
    mybatis-spring-1.2.2.jar
    mysql-connector-java-5.1.10-bin.jar
    spring-aop-4.1.6.RELEASE.jar
    spring-aspects-4.1.6.RELEASE.jar
    spring-beans-4.1.6.RELEASE.jar
    spring-context-4.1.6.RELEASE.jar
    spring-context-support-4.1.6.RELEASE.jar
    spring-core-4.1.6.RELEASE.jar
    spring-expression-4.1.6.RELEASE.jar
    spring-instrument-4.1.6.RELEASE.jar
    spring-instrument-tomcat-4.1.6.RELEASE.jar
    spring-jdbc-4.1.6.RELEASE.jar
    spring-jms-4.1.6.RELEASE.jar
    spring-messaging-4.1.6.RELEASE.jar
    spring-orm-4.1.6.RELEASE.jar
    spring-oxm-4.1.6.RELEASE.jar
    spring-test-4.1.6.RELEASE.jar
    spring-tx-4.1.6.RELEASE.jar
    spring-web-4.1.6.RELEASE.jar
    spring-webmvc-4.1.6.RELEASE.jar
    spring-webmvc-portlet-4.1.6.RELEASE.jar
    spring-websocket-4.1.6.RELEASE.jar



  • 相关阅读:
    自己修改的两个js文件
    .net4缓存笔记
    使用.net的Cache框架快速实现Cache操作
    关于招聘面试(转)
    PHP中获取当前页面的完整URL
    Linux在本地使用yum安装软件(转)
    Phalcon的学习篇-phalcon和devtools的安装和设置
    GY的实验室
    aip接口中对url参数md5加密防篡改的原理
    nginx 多站点配置方法集合(转)
  • 原文地址:https://www.cnblogs.com/stono/p/4566321.html
Copyright © 2011-2022 走看看