你还不知道spring如何集成myabtis的吗?我来教你使用SqlSessionTemplate的方式调用myabtis!!!

此项目是通过DAO层接口实现类的方式来调用mybatis的

需要注意mybatis的版本和spring-mybatis的兼容性问题
出现java.lang.NoClassDefFoundError: org/apache/ibatis/cursor/Cursor 错误!
说明是版本兼容问题,解决办法:mybatis的3.4.0及以上版本用mybatis-spring1.3.0及以上版本;mybatis的3.4.0以下版本用mybatis-spring1.3.0以下版本。

1.项目结构图

你还不知道spring如何集成myabtis的吗?我来教你使用SqlSessionTemplate的方式调用myabtis!!!_第1张图片

2.maven依赖

<?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>org.suda</groupId>
  <artifactId>mybatisStudy_day03</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>mybatisStudy_day03 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>5.2.5.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl-api</artifactId>
      <version>1.2</version>
      <exclusions>
        <exclusion>
          <artifactId>jsp-api</artifactId>
          <groupId>javax.servlet.jsp</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl-api</artifactId>
      <version>1.2</version>
      <exclusions>
        <exclusion>
          <artifactId>jsp-api</artifactId>
          <groupId>javax.servlet.jsp</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.0</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.4.0</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.28</version>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.5</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
      <exclusions>
        <exclusion>
          <artifactId>jackson-annotations</artifactId>
          <groupId>com.fasterxml.jackson.core</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.5</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-jaxb-annotations</artifactId>
      <version>2.9.5</version>
      <exclusions>
        <exclusion>
          <artifactId>jackson-annotations</artifactId>
          <groupId>com.fasterxml.jackson.core</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <!-- 分页插件 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.2.9</version>
      <exclusions>
        <exclusion>
          <artifactId>spring-boot-starter</artifactId>
          <groupId>org.springframework.boot</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-boot-autoconfigure</artifactId>
          <groupId>org.springframework.boot</groupId>
        </exclusion>
        <exclusion>
          <artifactId>mybatis-spring</artifactId>
          <groupId>org.mybatis</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-jdbc</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>mybatis</artifactId>
          <groupId>org.mybatis</groupId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <finalName>mybatisStudy_day03</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

3.index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <a href="<%=request.getContextPath()%>/userController/queryUserAll">点击查询用户信息</a>
</body>
</html>

4.web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0"
         metadata-complete="true">

  <display-name>Archetype Created Web Application</display-name>

  <!--设置配置文件的路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <!--配置spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--配置dispatcherServlet-->
  <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-servlet.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--配置post请求方式携带中文时的编码格式-->
  <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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>
</web-app>

5.springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:http="http://www.springframework.org/schema/c"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置自动化扫描机制,只扫描controlle注解类-->
    <context:component-scan base-package="com.mybatisStudy.**.web">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--启用MVC配置-->
    <mvc:annotation-driven/>

    <!--配置视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/com/orderManag"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 允许访问静态文件-->
<!--    <mvc:resources mapping="/css/**" location="/css/"/>-->
<!--    <mvc:resources mapping="/js/**" location="/js/"/>-->
<!--    <mvc:resources mapping="/icons/**" location="/icons/"/>-->
<!--    <mvc:resources mapping="/image/**" location="/image/"/>-->
<!--    <mvc:resources mapping="/views/**/*" location="/views/"/>-->
<!--    <mvc:resources mapping="/layuiadmin/**/*" location="/layuiadmin/"/>-->
</beans>

6.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:http="http://www.springframework.org/schema/c"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置自动化扫描机制,controlle注解不扫描-->
    <context:component-scan base-package="com.mybatisStudy">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/order?useUnicode=true&characterEncoding=UTF-8"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。
         *是个通配符,代表所有的文件,**代表所有目录下 -->
<!--        <property name="mapperLocations">-->
<!--            <array>-->
<!--                <value>classpath:mybatisStudy/mybatisMappers/*.xml-->


    


    

        





    
    

7.mybatis-config.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>
    <!-- logImple属性配置指定使用LOG4J输出日志 -->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <mappers>
        <mapper resource="mybatisStudy/mybatisMappers/SysUserMapper.xml"></mapper>
    </mappers>

</configuration>

8.log4j.properties

#全局配置
log4j.rootLogger=info, stdout
#MyBatis 日志配置
log4j.logger.com.mybatisStudy.dao=TRACE
#控制台输出配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %d{yyyy-MM-dd HH:mm:ss} <%p> %l %m%n

9.SysUserMapper.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.mybatisStudy.dao.IUserDao">
  <resultMap id="BaseResultMap" type="com.mybatisStudy.vo.UserVO">
    <id column="ID" jdbcType="VARCHAR" property="id" />
    <result column="USER_NUM" jdbcType="VARCHAR" property="userNum" />
    <result column="USER_NAME" jdbcType="VARCHAR" property="userName" />
    <result column="PASSWORD" jdbcType="VARCHAR" property="password" />
    <result column="USER_MOTTO" jdbcType="VARCHAR" property="userMotto" />
    <result column="TELEPHONE" jdbcType="VARCHAR" property="telephone" />
    <result column="ADDRESS" jdbcType="VARCHAR" property="address" />
    <result column="EMAIL" jdbcType="VARCHAR" property="eMail" />
    <result column="ICON_NUM" jdbcType="VARCHAR" property="iconNum" />
    <result column="USER_SEX" jdbcType="CHAR" property="userSex" />
    <result column="LAST_SIGIN_TIME" jdbcType="DATE" property="lastSiginTime" />
    <result column="LOGIN_NUMBERS" jdbcType="INTEGER" property="loginNumbers" />
    <result column="CREATE_NAME" jdbcType="VARCHAR" property="createName" />
    <result column="CREATE_NUM" jdbcType="VARCHAR" property="createNum" />
    <result column="UPDATE_NAME" jdbcType="VARCHAR" property="updateName" />
    <result column="UPDATE_NUM" jdbcType="VARCHAR" property="updateNum" />
    <result column="CREAT_TIME" jdbcType="DATE" property="creatTime" />
    <result column="UPDATE_TIME" jdbcType="DATE" property="updateTime" />
    <result column="STATE" jdbcType="CHAR" property="state" />
  </resultMap>
  <sql id="Base_Column_List">
    ID, USER_NUM, USER_NAME, PASSWORD, USER_MOTTO, TELEPHONE, ADDRESS, EMAIL, ICON_NUM, 
    USER_SEX, LAST_SIGIN_TIME, LOGIN_NUMBERS, CREATE_NAME, CREATE_NUM, UPDATE_NAME, UPDATE_NUM, 
    CREAT_TIME, UPDATE_TIME, STATE
  </sql>

  <select id="queryUserAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from sys_user
  </select>

  <select id="queryUserAllOffPage" resultMap="BaseResultMap">
    select
        ID, USER_NUM, USER_NAME, PASSWORD, USER_MOTTO, TELEPHONE, ADDRESS, EMAIL, ICON_NUM,
        USER_SEX, LAST_SIGIN_TIME, LOGIN_NUMBERS, CREATE_NAME, CREATE_NUM, UPDATE_NAME, UPDATE_NUM,
        CREAT_TIME, UPDATE_TIME, STATE
    from sys_user
    <where>
      <if test="userNum != null and userNum != ''"> and USER_NUM = #{userNum} </if>
      <if test="userName != null and userName != ''"> and USER_NAME like concat(concat('%',#{userName}),'%')</if>
      <if test="userSex != null and userSex != ''"> and USER_SEX = #{userSex} </if>
    </where>
    limit #{startIndex},#{queryCount}
  </select>

  <select id="queryUserAllOffPageCount" resultType="int">
    select count(id) from sys_user
    <where>
      <if test="userNum != null and userNum != ''"> and USER_NUM = #{userNum} </if>
      <if test="userName != null and userName != ''"> and USER_NAME like concat(concat('%',#{userName}),'%')</if>
      <if test="userSex != null and userSex != ''"> and USER_SEX = #{userSex} </if>
    </where>
  </select>

  <select id="selectByPrimaryKey" parameterType="map" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from sys_user where ID = #{id,jdbcType=VARCHAR}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="map">
    delete from sys_user
    where ID = #{id,jdbcType=VARCHAR}
      and USER_NUM = #{userNum,jdbcType=VARCHAR}
  </delete>

  <delete id="deleteUserInfo" parameterType="java.lang.String">
    delete from sys_user where id in
    <foreach collection="array" item="id" open="(" close=")" separator="," index="i">
      #{id}
    </foreach>
  </delete>

  <insert id="insert" parameterType="com.mybatisStudy.vo.UserVO">
    insert into sys_user (ID, USER_NUM, USER_NAME, 
      PASSWORD, USER_MOTTO, TELEPHONE, 
      ADDRESS, EMAIL, ICON_NUM, 
      USER_SEX, LAST_SIGIN_TIME, LOGIN_NUMBERS, 
      CREATE_NAME, CREATE_NUM, UPDATE_NAME, 
      UPDATE_NUM, CREAT_TIME, UPDATE_TIME, 
      STATE)
    values (#{id,jdbcType=VARCHAR}, #{userNum,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, 
      #{password,jdbcType=VARCHAR}, #{userMotto,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR}, 
      #{address,jdbcType=VARCHAR}, #{eMail,jdbcType=VARCHAR}, #{iconNum,jdbcType=VARCHAR}, 
      #{userSex,jdbcType=CHAR}, #{lastSiginTime,jdbcType=DATE}, #{loginNumbers,jdbcType=INTEGER}, 
      #{createName,jdbcType=VARCHAR}, #{createNum,jdbcType=VARCHAR}, #{updateName,jdbcType=VARCHAR}, 
      #{updateNum,jdbcType=VARCHAR}, #{creatTime,jdbcType=DATE}, #{updateTime,jdbcType=DATE}, 
      #{state,jdbcType=CHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.mybatisStudy.vo.UserVO">
    insert into sys_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        ID,
      </if>
      <if test="userNum != null">
        USER_NUM,
      </if>
      <if test="userName != null">
        USER_NAME,
      </if>
      <if test="password != null">
        PASSWORD,
      </if>
      <if test="userMotto != null">
        USER_MOTTO,
      </if>
      <if test="telephone != null">
        TELEPHONE,
      </if>
      <if test="address != null">
        ADDRESS,
      </if>
      <if test="eMail != null">
        EMAIL,
      </if>
      <if test="iconNum != null">
        ICON_NUM,
      </if>
      <if test="userSex != null">
        USER_SEX,
      </if>
      <if test="lastSiginTime != null">
        LAST_SIGIN_TIME,
      </if>
      <if test="loginNumbers != null">
        LOGIN_NUMBERS,
      </if>
      <if test="createName != null">
        CREATE_NAME,
      </if>
      <if test="createNum != null">
        CREATE_NUM,
      </if>
      <if test="updateName != null">
        UPDATE_NAME,
      </if>
      <if test="updateNum != null">
        UPDATE_NUM,
      </if>
      <if test="creatTime != null">
        CREAT_TIME,
      </if>
      <if test="updateTime != null">
        UPDATE_TIME,
      </if>
      <if test="state != null">
        STATE,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="userNum != null">
        #{userNum,jdbcType=VARCHAR},
      </if>
      <if test="userName != null">
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="userMotto != null">
        #{userMotto,jdbcType=VARCHAR},
      </if>
      <if test="telephone != null">
        #{telephone,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="eMail != null">
        #{eMail,jdbcType=VARCHAR},
      </if>
      <if test="iconNum != null">
        #{iconNum,jdbcType=VARCHAR},
      </if>
      <if test="userSex != null">
        #{userSex,jdbcType=CHAR},
      </if>
      <if test="lastSiginTime != null">
        #{lastSiginTime,jdbcType=DATE},
      </if>
      <if test="loginNumbers != null">
        #{loginNumbers,jdbcType=INTEGER},
      </if>
      <if test="createName != null">
        #{createName,jdbcType=VARCHAR},
      </if>
      <if test="createNum != null">
        #{createNum,jdbcType=VARCHAR},
      </if>
      <if test="updateName != null">
        #{updateName,jdbcType=VARCHAR},
      </if>
      <if test="updateNum != null">
        #{updateNum,jdbcType=VARCHAR},
      </if>
      <if test="creatTime != null">
        #{creatTime,jdbcType=DATE},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=DATE},
      </if>
      <if test="state != null">
        #{state,jdbcType=CHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.mybatisStudy.vo.UserVO">
    update sys_user
    <set>
      <if test="userName != null">
        USER_NAME = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        PASSWORD = #{password,jdbcType=VARCHAR},
      </if>
      <if test="userMotto != null">
        USER_MOTTO = #{userMotto,jdbcType=VARCHAR},
      </if>
      <if test="telephone != null">
        TELEPHONE = #{telephone,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        ADDRESS = #{address,jdbcType=VARCHAR},
      </if>
      <if test="eMail != null">
        EMAIL = #{eMail,jdbcType=VARCHAR},
      </if>
      <if test="iconNum != null">
        ICON_NUM = #{iconNum,jdbcType=VARCHAR},
      </if>
      <if test="userSex != null">
        USER_SEX = #{userSex,jdbcType=CHAR},
      </if>
      <if test="lastSiginTime != null">
        LAST_SIGIN_TIME = #{lastSiginTime,jdbcType=DATE},
      </if>
      <if test="loginNumbers != null">
        LOGIN_NUMBERS = #{loginNumbers,jdbcType=INTEGER},
      </if>
      <if test="createName != null">
        CREATE_NAME = #{createName,jdbcType=VARCHAR},
      </if>
      <if test="createNum != null">
        CREATE_NUM = #{createNum,jdbcType=VARCHAR},
      </if>
      <if test="updateName != null">
        UPDATE_NAME = #{updateName,jdbcType=VARCHAR},
      </if>
      <if test="updateNum != null">
        UPDATE_NUM = #{updateNum,jdbcType=VARCHAR},
      </if>
      <if test="creatTime != null">
        CREAT_TIME = #{creatTime,jdbcType=DATE},
      </if>
      <if test="updateTime != null">
        UPDATE_TIME = #{updateTime,jdbcType=DATE},
      </if>
      <if test="state != null">
        STATE = #{state,jdbcType=CHAR},
      </if>
    </set>
    where ID = #{id,jdbcType=VARCHAR}
      and USER_NUM = #{userNum,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.mybatisStudy.vo.UserVO">
    update sys_user
    set USER_NAME = #{userName,jdbcType=VARCHAR},
      PASSWORD = #{password,jdbcType=VARCHAR},
      USER_MOTTO = #{userMotto,jdbcType=VARCHAR},
      TELEPHONE = #{telephone,jdbcType=VARCHAR},
      ADDRESS = #{address,jdbcType=VARCHAR},
      EMAIL = #{eMail,jdbcType=VARCHAR},
      ICON_NUM = #{iconNum,jdbcType=VARCHAR},
      USER_SEX = #{userSex,jdbcType=CHAR},
      LAST_SIGIN_TIME = #{lastSiginTime,jdbcType=DATE},
      LOGIN_NUMBERS = #{loginNumbers,jdbcType=INTEGER},
      CREATE_NAME = #{createName,jdbcType=VARCHAR},
      CREATE_NUM = #{createNum,jdbcType=VARCHAR},
      UPDATE_NAME = #{updateName,jdbcType=VARCHAR},
      UPDATE_NUM = #{updateNum,jdbcType=VARCHAR},
      CREAT_TIME = #{creatTime,jdbcType=DATE},
      UPDATE_TIME = #{updateTime,jdbcType=DATE},
      STATE = #{state,jdbcType=CHAR}
    where ID = #{id,jdbcType=VARCHAR}
      and USER_NUM = #{userNum,jdbcType=VARCHAR}
  </update>
</mapper>

10.UserController.java

@Controller
@RequestMapping("/userController")
public class UserController {
    private static final Logger log = Logger.getLogger("UserController.class");

    @Autowired
    @Qualifier("userServiceImpl")
    private IUserService userServiceImpl;

    @RequestMapping(value = "/queryUserAll",method = RequestMethod.GET)
    public void queryUserAll(){
        log.info("UserController queryUserAll begin");
        userServiceImpl.queryUserAll();
    }
}

11.IUserService.java

public interface IUserService {

    void queryUserAll();
}

12.UserServiceImpl.java

@Service(value = "userServiceImpl")
public class UserServiceImpl implements IUserService {

    private static final Logger log = Logger.getLogger("UserServiceImpl.class");

    @Autowired
    private IUserDao userDaoImpl;

    @Override
    public void queryUserAll() {
        log.info("UserServiceImpl queryUserAll begin");
        List<UserVO> userVOList = userDaoImpl.queryUserAll();
        if(null != userVOList && userVOList.size()>0){
            for(UserVO userVo : userVOList){
                System.out.println(userVo.toString());
            }
        }
    }
}

13.IUserDao.java

public interface IUserDao {
    int deleteUserInfo(String[] idArray);

    int deleteByPrimaryKey(@Param("id") String id, @Param("userNum") String userNum);

    int insert(UserVO record);

    int insertSelective(UserVO record);

    UserVO selectByPrimaryKey(@Param("id") String id);

    List<UserVO> queryUserAll();

    List<UserVO> queryUserAllOffPage(UserVO UserVO);

    Integer queryUserAllOffPageCount(UserVO UserVO);

    int updateByPrimaryKeySelective(UserVO record);

    int updateByPrimaryKey(UserVO record);
}

14.UserDaoImpl.java

@Repository("userDaoImpl")
public class UserDaoImpl implements IUserDao {
    private static final Logger log = Logger.getLogger("UserDaoImpl.class");

    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    @Override
    public int deleteUserInfo(String[] idArray) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(String id, String userNum) {
        return 0;
    }

    @Override
    public int insert(UserVO record) {
        return 0;
    }

    @Override
    public int insertSelective(UserVO record) {
        return 0;
    }

    @Override
    public UserVO selectByPrimaryKey(String id) {
        return null;
    }

    @Override
    public List<UserVO> queryUserAll() {
        log.info("userDaoImpl queryUserAll begin");
        List<UserVO> userVOList = sqlSessionTemplate.selectList("com.mybatisStudy.dao.IUserDao.queryUserAll");
        return null;
    }

    @Override
    public List<UserVO> queryUserAllOffPage(UserVO UserVO) {
        return null;
    }

    @Override
    public Integer queryUserAllOffPageCount(UserVO UserVO) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(UserVO record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(UserVO record) {
        return 0;
    }
}

15.UserVO.java

package com.mybatisStudy.vo;

import java.util.Date;

public class UserVO {
    private String id;

    private String userNum;

    private String userName;

    private String password;

    private String userMotto;

    private String telephone;

    private String address;

    private String eMail;

    private String iconNum;

    private String userSex;

    private Date lastSiginTime;

    private Integer loginNumbers;

    private String createName;

    private String createNum;

    private String updateName;

    private String updateNum;

    private Date creatTime;

    private Date updateTime;

    private String state;

    private static final long serialVersionUID = 1L;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    public String getUserNum() {
        return userNum;
    }

    public void setUserNum(String userNum) {
        this.userNum = userNum == null ? null : userNum.trim();
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getUserMotto() {
        return userMotto;
    }

    public void setUserMotto(String userMotto) {
        this.userMotto = userMotto == null ? null : userMotto.trim();
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone == null ? null : telephone.trim();
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address == null ? null : address.trim();
    }

    public String getEMail() {
        return eMail;
    }

    public void setEMail(String eMail) {
        this.eMail = eMail == null ? null : eMail.trim();
    }

    public String getIconNum() {
        return iconNum;
    }

    public void setIconNum(String iconNum) {
        this.iconNum = iconNum == null ? null : iconNum.trim();
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex = userSex == null ? null : userSex.trim();
    }

    public Date getLastSiginTime() {
        return lastSiginTime;
    }

    public void setLastSiginTime(Date lastSiginTime) {
        this.lastSiginTime = lastSiginTime;
    }

    public Integer getLoginNumbers() {
        return loginNumbers;
    }

    public void setLoginNumbers(Integer loginNumbers) {
        this.loginNumbers = loginNumbers;
    }

    public String getCreateName() {
        return createName;
    }

    public void setCreateName(String createName) {
        this.createName = createName == null ? null : createName.trim();
    }

    public String getCreateNum() {
        return createNum;
    }

    public void setCreateNum(String createNum) {
        this.createNum = createNum == null ? null : createNum.trim();
    }

    public String getUpdateName() {
        return updateName;
    }

    public void setUpdateName(String updateName) {
        this.updateName = updateName == null ? null : updateName.trim();
    }

    public String getUpdateNum() {
        return updateNum;
    }

    public void setUpdateNum(String updateNum) {
        this.updateNum = updateNum == null ? null : updateNum.trim();
    }

    public Date getCreatTime() {
        return creatTime;
    }

    public void setCreatTime(Date creatTime) {
        this.creatTime = creatTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state == null ? null : state.trim();
    }

    @Override
    public String toString() {
        return "UserVO{" +
                "id='" + id + '\'' +
                ", userNum='" + userNum + '\'' +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", userMotto='" + userMotto + '\'' +
                ", telephone='" + telephone + '\'' +
                ", address='" + address + '\'' +
                ", eMail='" + eMail + '\'' +
                ", iconNum='" + iconNum + '\'' +
                ", userSex='" + userSex + '\'' +
                ", lastSiginTime=" + lastSiginTime +
                ", loginNumbers=" + loginNumbers +
                ", createName='" + createName + '\'' +
                ", createNum='" + createNum + '\'' +
                ", updateName='" + updateName + '\'' +
                ", updateNum='" + updateNum + '\'' +
                ", creatTime=" + creatTime +
                ", updateTime=" + updateTime +
                ", state='" + state + '\'' +
                '}';
    }
}

16.SqlSessionTemplate组件解析

严格来说SqlSessionTemplate组件并不是一个必须配置的组件,但是他也存在一定的价值。首先,它是线程安全的类,也就是确保每个线程使用的sqlSeesion唯一且不互相冲突。其次,它提供了一系列的功能,比如增删改等常用的功能,不过在使用SqlSessionTemplate之前需要在applicationContext.xml文件中配置

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--         通过带有参数的构造方法创建该对象-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
<!--        myBatis执行器类型 SIMPLE,REUSE,BATCH-->
<!--        SIMPLE:这个执行器类型不做特殊的事情。它为每个语句的执行创建一个新的预处理语句。-->
<!--        REUSE:这个执行器类型会复用预处理语句。-->
<!--        BATCH:这个执行器会批量执行所有更新语句,如果 SELECT 在它们中间执行还会标定它们是必须的,来保证一个简单并易于理解的行为。-->
<!--        <constructor-arg index="1" value="BATCH"/>-->
    </bean>

SqlSessionTemplate要通过带有参数的构造方法去创建对象,常用的参数是SqlSessionFactory和MyBatis执行器(Executor)类型,取值范围是SIMPLE,REUSE,BATCH。配置完成后我们就可以在Dao层的实现类中通过自动注入的方式获取SqlSessionTemplate,例如

    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    @Override
    public List<UserVO> queryUserAll() {
        log.info("userDaoImpl queryUserAll begin");
        List<UserVO> userVOList = sqlSessionTemplate.selectList("com.mybatisStudy.dao.IUserDao.queryUserAll");
        return null;
    }

你可能感兴趣的:(spring和数据库编程)