欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)

程序员文章站 2022-06-06 11:51:38
项目结构: (代码里面都有注释) 一、在pom文件中依赖jar包 因为我这里分了模块,所以有父子级的共两个pom文件 父级:

项目结构:

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)

 

 (代码里面都有注释)

一、在pom文件中依赖jar包

因为我这里分了模块,所以有父子级的共两个pom文件

父级:

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
         xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>

    <groupid>edu.nf</groupid>
    <artifactid>conformity</artifactid>
    <version>1.0-snapshot</version>
    <modules>
        <module>ch01</module>
        <module>ch02</module>
        <module>ch03</module>
        <module>ch04</module>
        <module>ch05</module>
    </modules>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceencoding>utf-8</project.build.sourceencoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerversion>1.8</maven.compiler.compilerversion>
        <!-- 版本管理 -->
        <spring.version>5.1.1.release</spring.version>
        <junit.version>4.12</junit.version>
        <druid.version>1.1.10</druid.version>
        <mysql.version>5.1.47</mysql.version>
        <mybatis.version>3.4.6</mybatis.version>
        <mybatis.spring.version>1.3.2</mybatis.spring.version>
        <pagehelper.version>5.1.2</pagehelper.version>
        <aspect.version>1.9.1</aspect.version>
        <servlet.version>4.0.1</servlet.version>
        <jackson.version>2.9.6</jackson.version>
        <slf4j.version>1.7.25</slf4j.version>
    </properties>

    <!-- 依赖管理,子模块按需依赖 -->
    <dependencymanagement>
        <dependencies>
            <!-- javax.servlet-api -->
            <dependency>
                <groupid>javax.servlet</groupid>
                <artifactid>javax.servlet-api</artifactid>
                <version>${servlet.version}</version>
                <scope>provided</scope>
            </dependency>
            <!-- spring-web -->
            <dependency>
                <groupid>org.springframework</groupid>
                <artifactid>spring-web</artifactid>
                <version>${spring.version}</version>
            </dependency>
            <!-- spring-webmvc -->
            <dependency>
                <groupid>org.springframework</groupid>
                <artifactid>spring-webmvc</artifactid>
                <version>${spring.version}</version>
            </dependency>
            <!-- jackson-databind -->
            <dependency>
                <groupid>com.fasterxml.jackson.core</groupid>
                <artifactid>jackson-databind</artifactid>
                <version>${jackson.version}</version>
            </dependency>
            <!-- spring-jdbc -->
            <dependency>
                <groupid>org.springframework</groupid>
                <artifactid>spring-jdbc</artifactid>
                <version>${spring.version}</version>
            </dependency>
            <!-- spring-tx -->
            <dependency>
                <groupid>org.springframework</groupid>
                <artifactid>spring-tx</artifactid>
                <version>${spring.version}</version>
            </dependency>
            <!-- mybatis-->
            <dependency>
                <groupid>org.mybatis</groupid>
                <artifactid>mybatis</artifactid>
                <version>${mybatis.version}</version>
            </dependency>
            <!-- mybatis整合spring插件 -->
            <dependency>
                <groupid>org.mybatis</groupid>
                <artifactid>mybatis-spring</artifactid>
                <version>${mybatis.spring.version}</version>
            </dependency>
            <!-- pagehelper分页插件-->
            <dependency>
                <groupid>com.github.pagehelper</groupid>
                <artifactid>pagehelper</artifactid>
                <version>${pagehelper.version}</version>
            </dependency>
            <!-- aspectjweaver-->
            <dependency>
                <groupid>org.aspectj</groupid>
                <artifactid>aspectjweaver</artifactid>
                <version>${aspect.version}</version>
            </dependency>
        </dependencies>
    </dependencymanagement>

    <!-- 公共依赖 -->
    <dependencies>
        <!-- spring-context -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <!-- slf4j-log4j12 -->
        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-log4j12</artifactid>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- junit -->
        <dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>${junit.version}</version>
        </dependency>
        <!-- druid -->
        <dependency>
            <groupid>com.alibaba</groupid>
            <artifactid>druid</artifactid>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <version>${mysql.version}</version>
        </dependency>
    </dependencies>
</project>
view code

子级:

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
         xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactid>conformity</artifactid>
        <groupid>edu.nf</groupid>
        <version>1.0-snapshot</version>
    </parent>
    <modelversion>4.0.0</modelversion>
    <artifactid>ch04</artifactid>
    <packaging>war</packaging>

    <dependencies>
        <!-- javax.servlet-api -->
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>javax.servlet-api</artifactid>
        </dependency>
        <!-- spring-webmvc -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-webmvc</artifactid>
        </dependency>
        <!-- jackson -->
        <dependency>
            <groupid>com.fasterxml.jackson.core</groupid>
            <artifactid>jackson-databind</artifactid>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupid>org.mybatis</groupid>
            <artifactid>mybatis</artifactid>
        </dependency>
        <!-- mybatis整合spring插件 -->
        <dependency>
            <groupid>org.mybatis</groupid>
            <artifactid>mybatis-spring</artifactid>
        </dependency>
        <!-- 分页插件-->
        <dependency>
            <groupid>com.github.pagehelper</groupid>
            <artifactid>pagehelper</artifactid>
        </dependency>
        <!-- spring-jdbc-->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jdbc</artifactid>
        </dependency>
        <!-- spring-tx-->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-tx</artifactid>
        </dependency>
        <!-- aspectjweaver -->
        <dependency>
            <groupid>org.aspectj</groupid>
            <artifactid>aspectjweaver</artifactid>
        </dependency>
    </dependencies>

    <!-- war插件 -->
    <build>
        <plugins>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-war-plugin</artifactid>
                <version>2.2</version>
                <configuration>
                    <warsourcedirectory>web</warsourcedirectory>
                    <!-- 指定web.xml路径 -->
                    <webxml>web\web-inf\web.xml</webxml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
view code

ssm所需jar包:

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)

 

二、配置web.xml文件

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
         xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- 当初始化不同的配置文件时,spring会初始化不同的容器对象,
     也就是会产生父子容器,子容器可以共享父容器的内容,但父容器是不能
     访问子容器的对象-->

    <!-- 配置监听器去初始化applicationcontext.xml -->
    <listener>
        <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
    </listener>
    <!-- 配置监听器的初始化参数-->
    <context-param>
        <param-name>contextconfiglocation</param-name>
        <param-value>classpath:applicationcontext.xml</param-value>
    </context-param>
    
    <!-- 配置spring提供的字符编码过滤器 -->
    <filter>
        <filter-name>encodingfilter</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>
        <!-- 让response的编码和request保持一致-->
        <init-param>
            <param-name>forceencoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingfilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- webstatfilter用于采集监控的数据-->
    <filter>
        <filter-name>druidwebstatfilter</filter-name>
        <filter-class>com.alibaba.druid.support.http.webstatfilter</filter-class>
        <init-param>
            <param-name>exclusions</param-name>
            <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>druidwebstatfilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置statviewservlet,用于启用监控视图 -->
    <servlet>
        <servlet-name>druidstatview</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.statviewservlet</servlet-class>
        <init-param>
            <!-- 用户名 -->
            <param-name>loginusername</param-name>
            <param-value>root</param-value>
        </init-param>
        <init-param>
            <!-- 密码 -->
            <param-name>loginpassword</param-name>
            <param-value>root</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>druidstatview</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>

    <!-- 配置核心控制器 -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
        <!-- 初始化mvc配置文件 -->
        <init-param>
            <param-name>contextconfiglocation</param-name>
            <param-value>classpath:dispatcher-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
view code

 

三、配置dispatcher-servlet.xml

创建springmvc核心配置文件

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 扫描相应的控制器组件,不包含service以及dao -->
    <context:component-scan base-package="edu.nf.ch04.controller"/>

    <!-- 启用mvc注解驱动-->
    <mvc:annotation-driven/>

    <!-- 静态资源处理-->
    <mvc:default-servlet-handler/>

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
        <property name="prefix" value="/web-inf/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>
view code

 

四、配置applicationcontext.xml

创建spring核心配置文件

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
<?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:context="http://www.springframework.org/schema/context"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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://mybatis.org/schema/mybatis-spring
       http://mybatis.org/schema/mybatis-spring.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 启用注解并扫描 -->
    <context:component-scan base-package="edu.nf.ch04">
        <!-- 排除控制层组件,也就是排除带有@controller注解的类 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller"/>
    </context:component-scan>

    <!-- 配置数据源连接池 -->
    <bean id="datasource" class="com.alibaba.druid.pool.druiddatasource" init-method="init" destroy-method="close">
        <!-- 数据库连接属性 -->
        <property name="url" value="jdbc:mysql://localhost:3306/homework?usessl=true&amp;useunicode=true&amp;characterencoding=utf-8"/>
        <property name="driverclassname" value="com.mysql.jdbc.driver"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
        <!-- 启用系统监控过滤器 -->
        <property name="filters" value="stat"/>
        <!-- 最大连接池数量 -->
        <property name="maxactive" value="200"/>
        <!-- 初始化时建立物理连接的个数-->
        <property name="initialsize" value="5"/>
        <!-- 最小连接池数量-->
        <property name="minidle" value="5"/>
        <!-- 获取连接时最大等待时间,单位毫秒-->
        <property name="maxwait" value="60000"/>
        <!-- 销毁线程会检测连接的间隔时间,(单位毫秒)
        如果连接空闲时间大于等于minevictableidletimemillis则关闭物理连接-->
        <property name="timebetweenevictionrunsmillis" value="60000"/>
        <!-- 连接保持空闲而不被驱逐的最小时间 -->
        <property name="minevictableidletimemillis" value="300000"/>
        <!--申请连接的时候检测,如果空闲时间大于timebetweenevictionrunsmillis,
        执行validationquery检测连接是否有效-->
        <property name="testwhileidle" value="true"/>
        <!-- 申请连接时执行validationquery检测连接是否有效 -->
        <property name="testonborrow" value="false"/>
        <!-- 归还连接时执行validationquery检测连接是否有效-->
        <property name="testonreturn" value="false"/>
        <!-- 是否缓存preparedstatement, mysql建议关闭-->
        <property name="poolpreparedstatements" value="false"/>
        <!-- 伪sql,用于检查连接是否可用 -->
        <property name="validationquery" value="select 1"/>
    </bean>

    <!-- 配置sqlsessionfactory,将mybatis的sqlsessionfactory交给spring容器管理 -->
    <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
        <!-- 注入druid数据源 -->
        <property name="datasource" ref="datasource"/>
        <!-- 给实体包下的类定义默认别名-->
        <property name="typealiasespackage" value="edu.nf.ch04.entity"/>
        <!-- 指定mapper映射配置文件的目录-->
        <property name="mapperlocations" value="classpath:mapper/*.xml"/>
        <!-- 配置分页插件-->
        <property name="plugins">
            <!-- 配置一个bean,指定分页插件的拦截器-->
            <bean class="com.github.pagehelper.pageinterceptor">
                <property name="properties">
                    <!--配置拦截器的参数属性 -->
                    <props>
                        <!-- 指定数据库方言 -->
                        <prop key="helperdialect">mysql</prop>
                        <!-- 分页参数使用注解支持-->
                        <prop key="supportmethodsarguments">true</prop>
                    </props>
                </property>
            </bean>
        </property>
    </bean>

    <!-- 指定扫描的dao接口的包,利用动态代理在运行时生成dao的实现类 -->
    <mybatis:scan base-package="edu.nf.ch04.dao"/>

    <!-- 装配datasource的事务管理器-->
    <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
        <!-- 需要注入一个datasource -->
        <property name="datasource" ref="datasource"/>
    </bean>

    <!-- 启用事务注解驱动,transaction-manager引用上面定义的事务管理器的id,
    如果id的名称就叫做transactionmanager,那么就不需要指定这个属性,因为是默认值-->
    <tx:annotation-driven transaction-manager="txmanager"/>
</beans>
view code

mapper:classdao.xml 数据库执行语句

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
<?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="edu.nf.ch04.dao.classdao">

    <resultmap id="classmap" type="classinfo">
        <id property="id" column="c_id"/>
        <result property="classname" column="c_name"/>
    </resultmap>

    <select id="listclassinfo" resultmap="classmap">
        select * from class_info
    </select>

    <select id="getclassbyid" parametertype="integer" resultmap="classmap">
        select * from class_info where c_id = #{id}
    </select>

    <insert id="saveclassinfo" parametertype="classinfo">
        insert into class_info(c_name) values(#{classname});
    </insert>

    <update id="updateclassinfo" parametertype="classinfo">
        update class_info set c_name = #{classname}
    </update>

</mapper>
view code

五、log4j日志的配置:

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
<?xml version="1.0" encoding="utf-8"?>
<!doctype log4j:configuration system "log4j.dtd">
<log4j:configuration>

    <!-- 指定日志的输出方式,consoleappender表示输出到控制台 -->
    <appender name="console" class="org.apache.log4j.consoleappender">
        <!-- 使用自定义布局-->
        <layout class="org.apache.log4j.patternlayout">
            <!-- 自定义布局转换参数说明
                 %d 日期格式
                 %p 输出级别
                 %t 输出当先执行线程名
                 %m 输出当先运行的方法名
                 %l 输出日志事件的发生位置,包括类名、发生的线程,以及在代码中的行数
                 %c 输出当前的类名
                 %m 输出日志内容
                 %n 换行-->
            <param name="conversionpattern" value="[%d{yyyy-mm-dd hh:mm:ss} %-5p] [%t] %l - %m%n" />
        </layout>
        <!-- 过滤输出范围-->
        <filter class="org.apache.log4j.varia.levelrangefilter">
            <param name="levelmin" value="debug" />
            <param name="levelmax" value="error" />
            <param name="acceptonmatch" value="true" />
        </filter>
    </appender>

    <!-- 配置log4j的root,默认所有日志都将以root的配置进行输出-->
    <root>
        <!-- 指定输出级别,log4j一共分为7个级别,由低到高分为all、debug、info、warn、error、fatal、off
             但官方建议使用debug、info、warn、error这4个级别。通过使用日志级别,可以控制应用程序中相应级别日志信息的输出。
             例如,如果使用了info级别,则应用程序中所有低于info级别的日志信息(如debug)将不会被打印出来。-->
        <priority value ="debug"/>
        <!-- 引用输出方式,ref对应appender的name -->
        <appender-ref ref="console"/>
    </root>

</log4j:configuration>
view code

 

六、编写代码

controller

basecontroller:

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
package edu.nf.ch04.controller;

import edu.nf.ch04.controller.vo.responsevo;

/**
 * @author wangl
 * @date 2018/11/12
 * 父级控制器
 * 封装了给子类调用的成功响应视图的方法
 */
public class basecontroller {

    protected responsevo success(){
        return new responsevo();
    }

    protected responsevo success(object value){
        responsevo vo = new responsevo();
        vo.setvalue(value);
        return vo;
    }

    protected responsevo success(integer code, object value){
        responsevo vo = new responsevo();
        vo.setcode(code);
        vo.setvalue(value);
        return vo;
    }
}
view code

classcontroller:

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
package edu.nf.ch04.controller;

import edu.nf.ch04.controller.vo.responsevo;
import edu.nf.ch04.entity.classinfo;
import edu.nf.ch04.servie.classservice;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.responsebody;

import java.util.list;

/**
 * @author wangl
 * @date 2018/11/12
 */
@controller
public class classcontroller extends basecontroller{

    /**
     * 日志对象
     */
    private static final logger logger = loggerfactory.getlogger(classcontroller.class);

    /**
     * 注入对象
     */
    @autowired
    private classservice classservice;

    @getmapping("/listclassinfo")
    @responsebody
    public responsevo listclassinfo(integer pagenum, integer pagesize){
        list<classinfo> list = classservice.listclassinfo(pagenum, pagesize);
        logger.info("查询用户列表: " + list.size());
        return success(list);
    }

    @postmapping("/add")
    public responsevo addclass(classinfo classinfo){
        classservice.saveclassinfo(classinfo);
        return success();
    }
}
view code

advice(globalexceptionhandler):异常处理类

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
package edu.nf.ch04.controller.advice;


import edu.nf.ch04.controller.vo.responsevo;
import edu.nf.ch04.servie.dataaccessexception;
import org.springframework.http.httpstatus;
import org.springframework.web.bind.annotation.controlleradvice;
import org.springframework.web.bind.annotation.exceptionhandler;
import org.springframework.web.bind.annotation.responsebody;

/**
 * @author wangl
 * @date 2018/11/12
 * 定义一个全局的异常处理类(类似一个切面)
 * 这个类中定义的所有方法都是异常处理方法,
 * 也可以理解为是全局的异常通知
 * value属性指定对哪些控制器进行切入拦截
 */
@controlleradvice
public class globalexceptionhandler {

    /**
     * 全局异常处理方法
     * 用于处理参数异常
     * @param e
     * @return
     */
    @exceptionhandler(dataaccessexception.class)
    @responsebody
    public responsevo handledataaccessexception(dataaccessexception e){
        responsevo vo = new responsevo();
        //设置相应状态码以及异常信息
        vo.setcode(httpstatus.internal_server_error.value());
        vo.setmessage(e.getmessage());
        return vo;
    }
}
view code

vo响应视图

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
package edu.nf.ch04.controller.vo;

import org.springframework.http.httpstatus;

/**
 * @author wangl
 * @date 2018/11/12
 * 响应视图
 */
public class responsevo {

    private integer code = httpstatus.ok.value();

    private string message;

    private object value;

    public integer getcode() {
        return code;
    }

    public void setcode(integer code) {
        this.code = code;
    }

    public string getmessage() {
        return message;
    }

    public void setmessage(string message) {
        this.message = message;
    }

    public object getvalue() {
        return value;
    }

    public void setvalue(object value) {
        this.value = value;
    }

    @override
    public string tostring() {
        return "responsevo{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", value=" + value +
                '}';
    }
}
view code

service

自定义异常dataaccessexception:

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
package edu.nf.ch04.servie;

/**
 * @author wangl
 * @date 2018/11/12
 */
public class dataaccessexception extends runtimeexception{

    public dataaccessexception(string message) {
        super(message);
    }

    public dataaccessexception(string message, throwable cause) {
        super(message, cause);
    }

    public dataaccessexception(throwable cause) {
        super(cause);
    }
}
view code

业务层 接口classservice

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
package edu.nf.ch04.servie;

import edu.nf.ch04.entity.classinfo;

import java.util.list;

/**
 * @author wangl
 * @date 2018/11/8
 */
public interface classservice {

    /**
     * 查询班级信息
     * @param pagenum
     * @param pagesize
     * @return
     */
    list<classinfo> listclassinfo(integer pagenum, integer pagesize);

    /**
     * 根据id查询班级信息
     * @param id
     * @return
     */
    classinfo getclassbyid(integer id);

    /**
     * 新建班级信息
     * @param classinfo
     */
    void saveclassinfo(classinfo classinfo);

    /**
     * 修改班级信息
     * @param classinfo
     */
    void updateclassinfo(classinfo classinfo);
}
view code

业务层 实现类classserviceimpl

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
package edu.nf.ch04.servie.impl;

import edu.nf.ch04.dao.classdao;
import edu.nf.ch04.entity.classinfo;
import edu.nf.ch04.servie.classservice;
import edu.nf.ch04.servie.dataaccessexception;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.propagation;
import org.springframework.transaction.annotation.transactional;

import java.util.list;

/**
 * @author wangl
 * @date 2018/11/8
 * @transactional 事务注解
 */
@service("classservice")
@transactional(rollbackfor = runtimeexception.class)
public class classserviceimpl implements classservice {

    @autowired
    private classdao classdao;

    @transactional(rollbackfor = runtimeexception.class, propagation = propagation.supports, readonly = true)
    @override
    public list<classinfo> listclassinfo(integer pagenum, integer pagesize) {
        return classdao.listclassinfo(pagenum, pagesize);
    }

    @transactional(rollbackfor = runtimeexception.class, propagation = propagation.supports, readonly = true)
    @override
    public classinfo getclassbyid(integer id) {
        return classdao.getclassbyid(id);
    }

    @override
    public void saveclassinfo(classinfo classinfo) {
        try {
            classdao.saveclassinfo(classinfo);
        } catch (runtimeexception e) {
            e.printstacktrace();
            throw new dataaccessexception("添加失败");
        }
    }

    @override
    public void updateclassinfo(classinfo classinfo) {
        try {
            classdao.updateclassinfo(classinfo);
        } catch (exception e) {
            e.printstacktrace();
            throw new runtimeexception("修改失败");
        }
    }
}
view code

dao(classdao持久层 接口)

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
package edu.nf.ch04.dao;

import edu.nf.ch04.entity.classinfo;
import org.apache.ibatis.annotations.param;
import org.springframework.stereotype.repository;

import java.util.list;

/**
 * @author wangl
 * @date 2018/11/8
 */
@repository
public interface classdao {

    /**
     * 查询班级列表信息
     * @param pagenum
     * @param pagesize
     * @return
     */
    list<classinfo> listclassinfo(@param("pagenum") integer pagenum, @param("pagesize") integer pagesize);

    /**
     * 根据id查询班级信息
     * @param id
     * @return
     */
    classinfo getclassbyid(integer id);

    /**
     * 新建班级信息
     * @param classinfo
     */
    void saveclassinfo(classinfo classinfo);

    /**
     * 修改班级信息
     * @param classinfo
     */
    void updateclassinfo(classinfo classinfo);
}
view code

entity(classinfo 实体类)

Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
package edu.nf.ch04.entity;

/**
 * @author wangl
 * @date 2018/11/8
 */
public class classinfo {

    private integer id;

    private string classname;

    public integer getid() {
        return id;
    }

    public void setid(integer id) {
        this.id = id;
    }

    public string getclassname() {
        return classname;
    }

    public void setclassname(string classname) {
        this.classname = classname;
    }

    @override
    public string tostring() {
        return "classinfo{" +
                "id=" + id +
                ", classname='" + classname + '\'' +
                '}';
    }
}
view code