• mybatis spring maven


    maven版本:3.3.9  解压即可使用

    spring版本:4.3.9  通过maven进行管理下载

    mybatis版本:3.4.4 通过maven进行管理下载

    mysql版本:5.7  connector也是通过maven进行下载

    本工程的git地址:https://github.com/WQZ321123/springDemo

    首先,使用maven一定要网速好一点,不然在线下载jar包会很慢,其次,关于仓库的问题,最好换成国内的阿里云的仓库,下载会更快。

    具体为,修改maven的E:Runtimeapache-maven-3.3.9conf下的settings.xml文件:

    在 <mirrors>标签内添加:

    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
      </mirror>
    
       <mirror>
        <id>CN</id>
        <name>OSChina Central</name>
        <url>http://maven.oschina.net/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
      </mirror>

    然后再eclipse中直接在线安装maven的插件,然后我们新建一个maven工程,选择:

    image

    以创建一个web工程。

    然后在,pom.xml中填入:

    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.audi</groupId>
        <artifactId>testMavenSpring</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>testMavenSpring Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.3.9.RELEASE</version>
                <!-- <scope>runtime</scope> -->
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.9.RELEASE</version>
                <!-- <scope>runtime</scope> -->
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.4</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>6.0.6</version>
            </dependency>
        </dependencies>
        <build>
            <finalName>testMavenSpring</finalName>
            <plugins>
                <!-- define the project compile level -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    保存以后,maven会自动开始下载jar包。

    这里遇到了一个问题:

    image

    全部的错误信息是:

    Description    Resource    Path    Location    Type
    Failure to transfer org.codehaus.plexus:plexus:pom:1.0.12 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.codehaus.plexus:plexus:pom:1.0.12 from/to central (https://repo.maven.apache.org/maven2): The operation was cancelled.    pom.xml    /testMavenSpring    line 1    Maven Configuration Problem

    解决方法如下:

    在maven的本地仓库中,比如我的路径是C:UsersMike.m2 epository下搜索last,

    image

    把这些文件全部删除了,然后在eclipse中,项目右键——》maven更新一下项目,上述错误就解决了。

    image

    如果需要将web module改成3.0版本的话,直接在项目右键——》properties——》project facets更改的话,可能会报错。

    image

    报错信息如下:

    Cannot change version of project facet Dynamic Web Module to 3.0

    这个时候你需要更改下图中选中的两个文件(两个文件都存在于当前工程路径下):

    image

    将java的版本改为1.6以上,将  <installed facet="jst.web" version="2.3"/>改为3.0

    同时,修改工程目录下的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="schedule-console" version="3.0">
    </web-app>

    这个时候,项目右键——》properties——》project facets中的dynamic web module应该自动变成3.0了,如果没变就重启一下eclipse就可以了。

    mysql的自动增长AUTO_INCREMENT,在我们使用delete from table_name语句删除了表中全部的数据以后,下一次在插入一条数据,那么自增长依然会按照删除之前自增长的值来增长,并不会回到默认值(并且,这个和使用哪种存储引擎是没有关系的),解决的办法就是直接使用truncate table table_name,这样删除数据自增长字段就可以回到初始的值了。

    关于存储引擎的一片文章,http://www.cnblogs.com/gbyukg/archive/2011/11/09/2242271.html,写的很好。

    下面开始连接mysql

    参考文章:http://blog.csdn.net/techbirds_bao/article/details/9233599/

    使用mysql作为数据库,新建一个数据库名为mybatis,下面是建表语句:

    #mysql注释需要注意一点 在sql文件中最好使用#来进行注释,因为使用-- 来注释需要在它后面加上一个空格  否则运行sql文件会报错
    #创建表
    Create TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `userName` varchar(50) DEFAULT NULL,
      `userAge` int(11) DEFAULT NULL,
      `userAddress` varchar(200) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
    #MySQL5.5以后默认使用InnoDB存储引擎,提供事务安全表,其它存储引擎都是非事务安全表。
    ALTER TABLE `user` COMMENT '测试用户表';
    ALTER TABLE `user` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id';
    ALTER TABLE `user` MODIFY `userName` varchar(50) COMMENT '用户';

    eclipse中项目需要做的一些配置如下:

    首先是一个映射文件User.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.audi.mybatis.UserMapper">
        <select id="selectUserByID" parameterType="int" resultType="User">
            select * from user where id = #{id}
        </select>
        
        <select id="selectUserAll" resultType="User">
            select * from user
        </select>
    </mapper>

    然后是连接数据库需要做的一些配置Configuration.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>
        <typeAliases> 
            <typeAlias alias="User" type="com.audi.mybatis.dto.User"/> 
        </typeAliases> 
    
        <environments default="development">
            <environment id="development">
            <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=UTC" />
                <property name="username" value="root"/>
                <property name="password" value="yourcode"/>
                </dataSource>
            </environment>
        </environments>
        
        <mappers>
            <mapper resource="mapper/User.xml"/>
        </mappers>
    </configuration>

    简历数据库持久化对象DTO:

    package com.audi.mybatis.dto;
    
    public class User
    {
        private int id;
        private String userName;
        private String userAge;
        private String userAddress;
    
        public int getId()
        {
            return id;
        }
    
        public void setId(int id)
        {
            this.id = id;
        }
    
        public String getUserName()
        {
            return userName;
        }
    
        public void setUserName(String userName)
        {
            this.userName = userName;
        }
    
        public String getUserAge()
        {
            return userAge;
        }
    
        public void setUserAge(String userAge)
        {
            this.userAge = userAge;
        }
    
        public String getUserAddress()
        {
            return userAddress;
        }
    
        public void setUserAddress(String userAddress)
        {
            this.userAddress = userAddress;
        }
    
        @Override
        public String toString()
        {
            return "User [id=" + id + ", userName=" + userName + ", userAge=" + userAge + ", userAddress=" + userAddress
                    + "]";
        }
    
    }

    下面是测试类,直接以java程序运行:

    package com.audi.test;
    
    import java.io.Reader;
    import java.util.Iterator;
    import java.util.List;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import com.audi.mybatis.dto.User;
    
    
    public class TestMybatis
    {
        private static SqlSessionFactory sqlSessionFactory;
        private static Reader reader;
    
        static
        {
            try
            {
                reader = Resources.getResourceAsReader("config/Configuration.xml");
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        public static SqlSessionFactory getSession()
        {
            return sqlSessionFactory;
        }
    
        public static void main(String[] args)
        {
            SqlSession session = sqlSessionFactory.openSession();
            try
            {
                User user = (User) session.selectOne("com.audi.mybatis.UserMapper.selectUserByID", 3);
                System.out.println(user.getUserAddress());
                System.out.println(user.getUserName());
                List<User> list = session.selectList("com.audi.mybatis.UserMapper.selectUserAll");
                Iterator<User> iterator= list.iterator();
                while (iterator.hasNext())
                {
                    System.out.println(iterator.next());
                }
            } finally
            {
                session.close();
            }
        }
    }

    现在运行程序应该就可以获得数据库保存的结果了,注意上面的语句可能会抛出空指针异常,这事因为数据库里没有数据,查询不到id为1的用户数据。

    接下来我们来进行spring集成,使用spring来管理一些bean,比如DataSource。以接口变成方式来访问数据库。

    先看一下整个工程的目录结构图:

    image

    spring的一个配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
        <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
            <property name="url"
                value="jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=UTC" />
            <property name="username" value="root" />
            <property name="password" value="yourcode" />
        </bean>
    
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--dataSource属性指定要用到的连接池 -->
            <property name="dataSource" ref="dataSource" />
            <!--configLocation属性指定mybatis的核心配置文件 -->
            <property name="configLocation" value="config/Configuration.xml" />
        </bean>
    
        <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
            <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
            <property name="sqlSessionFactory" ref="sqlSessionFactory" />
            <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象 -->
            <property name="mapperInterface" value="com.audi.mybatis.dao.IUserOperation" />
        </bean>
    
    </beans>

    Configuration.xml,这个是配置mybatis的一些属性的,注意其中关于数据源都注释了,这部分交给spring来管理了(注意下面配置文件的标签的先后顺序是不可以变化的,详情可以参看那张截图(来自 深入浅出MyBatis技术原理与实战)):

    <?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 alias="User" type="com.audi.mybatis.dto.User"/> 
        </typeAliases> 
    
        <!-- <environments default="development">
            <environment id="development">
            <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=UTC" />
                <property name="username" value="root"/>
                <property name="password" value="w513723"/>
                </dataSource>
            </environment>
        </environments> -->
        
        <mappers>
            <mapper resource="mapper/User.xml"/>
        </mappers>
    </configuration>

    image

    映射文件User.xml:(注意其中的namespace和上面的user.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.audi.mybatis.dao.IUserOperation">
        <select id="selectUserByID" parameterType="int" resultType="User">
            select * from user where id = #{id}
        </select>
        
        <select id="selectUserAll" resultType="User">
            select * from user
        </select>
    </mapper>

    接口超级简单:IUserOperation.java

    package com.audi.mybatis.dao;
    
    import com.audi.mybatis.dto.User;
    
    public interface IUserOperation
    {
        public User selectUserByID(int id);
    }

    下面是测试代码:

    package com.audi.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.audi.mybatis.dao.IUserOperation;
    import com.audi.mybatis.dto.User;
    public class MybatisSprintTest
    {
    
        private static ApplicationContext ctx;
    
        static
        {
            // 使用ClassPathXmlApplicationContext来加载spring的配置文件
            ctx = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        }
    
        public static void main(String[] args)
        {
            IUserOperation mapper = (IUserOperation) ctx.getBean("userMapper");
            // 测试id=1的用户查询,根据数据库中的情况,可以改成你自己的.
            System.out.println("得到用户id=3的用户信息");
            User user = mapper.selectUserByID(3);
            System.out.println(user);
        }
    
    }

    测试结果:

    image

    个人觉得这种面向接口的变成方式其实不好,因为映射文件会和接口形成强关联的关系,并且接口不写实现类就直接调用,总感觉怪怪的。

    关于jsp页面中文乱码以及相关知识的解释:http://www.cnblogs.com/chengkai/articles/2171848.html

    我的解决方案是,将jsp使用记事本打开,先保存成ANSI格式,再保存成utf-8格式就可以了。

    今天遇到了java文件也存在乱码的问题,解决方式和jsp乱码解决的方式相似,并且将整个workspace改成了utf-8编码格式,但是这个时候发现使用mvn install命令,如果输了中文就是乱码。

    乱码如下:

    解决办法是在pom.xml文件的<project>标签中加入:

    <properties>
            <argLine>-Dfile.encoding=UTF-8</argLine>
        </properties>

    然后再使用 mvn install就没有乱码了,效果如下。

    对于不能注入的问题,考虑在applicationContext.xml文件中加入:

    <!-- 配置扫描的包 -->
        <context:component-scan base-package="com.audi.*" />
    
        <!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
        <mvc:annotation-driven />

    接下来我们引入日志框架log4j。

    其实log4j还是很友好的,集成也比较简单,只需要在web.xml文件中加入如下配置信息:

    <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>config/log4j.properties</param-value>
        </context-param>
        <context-param>
            <param-name>log4jRefreshInterval</param-name>
            <param-value>60000</param-value>
        </context-param>
        <listener>
            <listener-class>
                org.springframework.web.util.Log4jConfigListener
            </listener-class>
        </listener>

    注意上面的listener是必须的,至少我的实践是这样的,没有listener的话,日志不会生效的。然后就是还需要一个日志的格式配置文件log4j.properties文件。

    log4j.rootLogger=INFO, stdout , R  
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
    #log4j.appender.stdout.layout.ConversionPattern=%d %p %t %c - %m%n  
    log4j.appender.stdout.layout.ConversionPattern=[%d{MM/dd HH:mm:ss}] [%-5p] %c{1}: - %m%n  
      
    log4j.appender.R=org.apache.log4j.RollingFileAppender  
    log4j.appender.R.File=d:/testMavenSpring.log  
    log4j.appender.R.MaxFileSize=1024KB  
    log4j.appender.R.MaxBackupIndex=10  
    log4j.appender.R.Encoding=utf-8
    log4j.appender.R.layout=org.apache.log4j.PatternLayout  
    log4j.appender.R.layout.ConversionPattern=[%d{MM/dd HH:mm:ss}] [%-5p] %c{1}: - %m%n  
    #log4j.appender.R.layout.ConversionPattern=%d %p %t %c - %m%n  
      
    log4j.logger.org.springframework=DEBUG  
    log4j.logger.org.mybatis=DEBUG  
    log4j.logger.java.sql.Connection=DEBUG
    log4j.logger.java.sql.Statement=DEBUG
    log4j.logger.java.sql.PreparedStatement=DEBUG
    log4j.logger.java.sql.ResultSet=DEBUG
    #必须加上下面这一句  否则sql语句不会打印出来
    log4j.logger.com.audi=debug

    现在整个工程的目录结构如下:

    image

    实际运行日志结果如下:

    image

    前端效果:

    image

    给mybatis配置事务支持

    网上大部分都会说,在applicationContext中添加如下一些代码:

    <!-- 数据库事务管理 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 使用注解来管理数据库事务操作 -->
        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    然后说在service层上使用@Transactional注解就可以使得事务生效了。

    其实这样不是很全面,事务可能不会生效。

    主要的日志信息如下:

    [07/10 15:18:44] [DEBUG] SqlSessionUtils: - Creating a new SqlSession
      [07/10 15:18:44] [DEBUG] SqlSessionUtils: - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46725c3] was not registered for synchronization because synchronization is not active
      [07/10 15:18:44] [DEBUG] DataSourceUtils: - Fetching JDBC Connection from DataSource
      [07/10 15:18:44] [DEBUG] SpringManagedTransaction: - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring
      [07/10 15:18:44] [DEBUG] insertUser: - ==>  Preparing: insert into user(id,userName,userAge,userAddress) VALUES(?,?,?,?) 
      [07/10 15:18:44] [DEBUG] insertUser: - ==> Parameters: 17(Integer), 王全洲(String), 100(String), 深圳市福田区岗厦(String)
      [07/10 15:18:44] [DEBUG] insertUser: - <==    Updates: 1
      [07/10 15:18:44] [DEBUG] SqlSessionUtils: - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46725c3]
      [07/10 15:18:44] [DEBUG] DataSourceUtils: - Returning JDBC Connection to DataSource
      [07/10 15:18:44] [DEBUG] SqlSessionUtils: - Creating a new SqlSession
      [07/10 15:18:44] [DEBUG] SqlSessionUtils: - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@291e6f56] was not registered for synchronization because synchronization is not active

    标黄的部分大意就说spring没有进行事务管理,没有生效。

    原因是因为我们在使用<context:component-scan配置注解的自动扫描的时候,多次扫描了service包造成的。

    解决的办法就是在spring-mvc的xml配置文件中,只扫描加载controller 对应的包就可以了,即springMVC-servlet.xml中使用:

    <context:component-scan base-package="com.audi.mybatis.controller" />

    applicationContext.xml中使用

    <context:component-scan base-package="com.audi.mybatis" />

    其实上面的两个配置也体现了,spring和springMVC稍微的一点不同,二者的配置都是应该要分开加载的。DAO和Service是属于spring的,Controller是属于SpringMVC的。

    完整的源代码:https://github.com/WQZ321123/springDemo

    关于spring的上下文配置信息,springMVC的上下文配置信息,加载顺序:

    先加载spring的上下文的配置信息,再在家springMVC的上下文配置信息。具体的可以参考http://blog.csdn.net/trigl/article/details/52073457

    接下来,我们给整个工程配置数据库的batch操作,即多个sql语句一次性执行,这里要说明的是我们不是在xml文件的某个id内连续写多个sql语句或者使用iterator关键字来拼装sql语句。而是在一个事务内的语句,我们一次性执行。

    需要做的配置是将数据库的连接url改为(后面的&allowMultiQueries=true就是新添加的):

    jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true

    然后在mybatis的配置文件中加入(注意这个配置标签是有位置要求的,具体顺序可以参照本博文上面的那张截图):

    <settings>
            <setting name="defaultExecutorType" value="BATCH" />
        </settings>

    然后,下面是不加batch操作的日志打印数据:

    [07/11 12:28:02] [DEBUG] DataSourceTransactionManager: - Creating new transaction with name [com.audi.mybatis.service.impl.UserServiceImpl.insertUser]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.Exception
      Tue Jul 11 12:28:03 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
    Tue Jul 11 12:28:03 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
    [07/11 12:28:03] [DEBUG] DataSourceTransactionManager: - Acquired Connection [jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true, UserName=root@localhost, MySQL Connector Java] for JDBC transaction
      [07/11 12:28:03] [DEBUG] DataSourceTransactionManager: - Switching JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true, UserName=root@localhost, MySQL Connector Java] to manual commit
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@623561ab] for key [org.apache.commons.dbcp.BasicDataSource@616af965] to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Initializing transaction synchronization
      [07/11 12:28:03] [DEBUG] TransactionInterceptor: - Getting transaction for [com.audi.mybatis.service.impl.UserServiceImpl.insertUser]
      [07/11 12:28:03] [DEBUG] SqlSessionUtils: - Creating a new SqlSession
      [07/11 12:28:03] [DEBUG] SqlSessionUtils: - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@63159810]
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Bound value [org.mybatis.spring.SqlSessionHolder@77acef79] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@6ccc4ab] to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@623561ab] for key [org.apache.commons.dbcp.BasicDataSource@616af965] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@623561ab] for key [org.apache.commons.dbcp.BasicDataSource@616af965] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] SpringManagedTransaction: - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true, UserName=root@localhost, MySQL Connector Java] will be managed by Spring
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@623561ab] for key [org.apache.commons.dbcp.BasicDataSource@616af965] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] insertUser: - ==>  Preparing: insert into user(id,userName,userAge,userAddress) VALUES(?,?,?,?) 
      [07/11 12:28:03] [DEBUG] insertUser: - ==> Parameters: 16(Integer), 王全洲(String), 100(String), 深圳市福田区岗厦(String)
      [07/11 12:28:03] [DEBUG] insertUser: - <==    Updates: 1
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@77acef79] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@6ccc4ab] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@77acef79] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@6ccc4ab] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] SqlSessionUtils: - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@63159810]
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@77acef79] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@6ccc4ab] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] SqlSessionUtils: - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@63159810] from current transaction
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@623561ab] for key [org.apache.commons.dbcp.BasicDataSource@616af965] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] insertUser: - ==>  Preparing: insert into user(id,userName,userAge,userAddress) VALUES(?,?,?,?) 
      [07/11 12:28:03] [DEBUG] insertUser: - ==> Parameters: 17(Integer), 王全洲(String), 100(String), 深圳市福田区岗厦(String)
      [07/11 12:28:03] [DEBUG] insertUser: - <==    Updates: 1
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@77acef79] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@6ccc4ab] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@77acef79] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@6ccc4ab] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] SqlSessionUtils: - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@63159810]
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@77acef79] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@6ccc4ab] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] SqlSessionUtils: - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@63159810] from current transaction
      [07/11 12:28:03] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@623561ab] for key [org.apache.commons.dbcp.BasicDataSource@616af965] bound to thread [http-nio-8080-exec-3]
      [07/11 12:28:03] [DEBUG] insertUser: - ==>  Preparing: insert into user(id,userName,userAge,userAddress) VALUES(?,?,?,?) 
      [07/11 12:28:03] [DEBUG] insertUser: - ==> Parameters: 18(Integer), 王全洲(String), 100(String), 深圳市福田区岗厦(String)
      [07/11 12:28:03] [DEBUG] insertUser: - <==    Updates: 1

    可以看到我们虽然在一个事务内部,但是没执行一次更新sql,就会执行一次。

    接下来是,添加batch以后的日志输出:

    [07/11 12:48:56] [DEBUG] DataSourceTransactionManager: - Creating new transaction with name [com.audi.mybatis.service.impl.UserServiceImpl.insertUser]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.Exception
      Tue Jul 11 12:48:56 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
    Tue Jul 11 12:48:56 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
    [07/11 12:48:56] [DEBUG] DataSourceTransactionManager: - Acquired Connection [jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true, UserName=root@localhost, MySQL Connector Java] for JDBC transaction
      [07/11 12:48:56] [DEBUG] DataSourceTransactionManager: - Switching JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true, UserName=root@localhost, MySQL Connector Java] to manual commit
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@4a1198e0] for key [org.apache.commons.dbcp.BasicDataSource@21d702b3] to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Initializing transaction synchronization
      [07/11 12:48:56] [DEBUG] TransactionInterceptor: - Getting transaction for [com.audi.mybatis.service.impl.UserServiceImpl.insertUser]
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Creating a new SqlSession
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@23f583c4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Bound value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@4a1198e0] for key [org.apache.commons.dbcp.BasicDataSource@21d702b3] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@4a1198e0] for key [org.apache.commons.dbcp.BasicDataSource@21d702b3] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] SpringManagedTransaction: - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true, UserName=root@localhost, MySQL Connector Java] will be managed by Spring
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@4a1198e0] for key [org.apache.commons.dbcp.BasicDataSource@21d702b3] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] insertUser: - ==>  Preparing: insert into user(id,userName,userAge,userAddress) VALUES(?,?,?,?) 
      [07/11 12:48:56] [DEBUG] insertUser: - ==> Parameters: 16(Integer), 王全洲(String), 100(String), 深圳市福田区岗厦(String)
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@23f583c4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@23f583c4] from current transaction
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@4a1198e0] for key [org.apache.commons.dbcp.BasicDataSource@21d702b3] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] insertUser: - ==> Parameters: 17(Integer), 王全洲(String), 100(String), 深圳市福田区岗厦(String)
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@23f583c4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@23f583c4] from current transaction
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@4a1198e0] for key [org.apache.commons.dbcp.BasicDataSource@21d702b3] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] insertUser: - ==> Parameters: 18(Integer), 王全洲(String), 100(String), 深圳市福田区岗厦(String)
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@23f583c4]
      [07/11 12:48:56] [DEBUG] TransactionInterceptor: - Completing transaction for [com.audi.mybatis.service.impl.UserServiceImpl.insertUser]
      [07/11 12:48:56] [DEBUG] DataSourceTransactionManager: - Triggering beforeCommit synchronization
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@23f583c4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@4a1198e0] for key [org.apache.commons.dbcp.BasicDataSource@21d702b3] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] DataSourceTransactionManager: - Triggering beforeCompletion synchronization
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@23f583c4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Removed value [org.mybatis.spring.SqlSessionHolder@2dafa469] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3d89aecc] from thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] SqlSessionUtils: - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@23f583c4]
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@4a1198e0] for key [org.apache.commons.dbcp.BasicDataSource@21d702b3] bound to thread [http-nio-8080-exec-4]
      [07/11 12:48:56] [DEBUG] DataSourceTransactionManager: - Initiating transaction commit
      [07/11 12:48:56] [DEBUG] DataSourceTransactionManager: - Committing JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true, UserName=root@localhost, MySQL Connector Java]
      [07/11 12:48:56] [DEBUG] DataSourceTransactionManager: - Triggering afterCommit synchronization
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Clearing transaction synchronization
      [07/11 12:48:56] [DEBUG] DataSourceTransactionManager: - Triggering afterCompletion synchronization
      [07/11 12:48:56] [DEBUG] TransactionSynchronizationManager: - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@4a1198e0] for key [org.apache.commons.dbcp.BasicDataSource@21d702b3] from thread [http-nio-8080-exec-4]
      [07/11 12:48:57] [DEBUG] DataSourceTransactionManager: - Releasing JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&allowMultiQueries=true, UserName=root@localhost, MySQL Connector Java] after transaction
      [07/11 12:48:57] [DEBUG] DataSourceUtils: - Returning JDBC Connection to DataSource

    从上面的日志我们可以看到,配置了batch以后,它不是一次执行一条,而是把每次循环的都当作参数,最后批量去数据库提交。

    项目静态资源的访问问题:

    dispatchServlet的配置如下:

    <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:spring-servlet.xml</param-value>
                     这里可以指定初始化servlet的xml文件的存放位置,多个配置文件使用逗号分开
            </init-param> -->
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springMVC</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    上面的dispatchServlet配置会拦截所有的请求,包括静态资源,这样会导致静态资源404,找不到。

    网上的解决办法都是大概的三种,我主要使用如下两种方式:

    <!-- 使用下面配置,告诉dispatchservlet不对静态资源进行映射 -->
        <!-- 方法1 -->
        <!-- <mvc:default-servlet-handler/> -->
        <!-- 方法2 -->
        <mvc:resources mapping="/static/**" location="/static/" />

    上面需要注意的static文件夹必须在webapp路径下:

    @U9A`TS@}@GQXKNJ2F9_IF8

    然后在.jsp页面中有两种方式引入静态资源,一种是通过

    <img src="${pageContext.request.contextPath}/static/image/testImg1.png"  alt="brainMaster" />

    一种是通过

    <script src="<c:url value='/static/js/service/user_service.js' />"></script>

    上面这种方式需要注意,要在jsp中加入如下代码才行,如下代码需要jstl1.2的jar包支持。

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

    spring的controller中返回string对象时,形如下面的形式

    public ResponseEntity<String> deleteUserById(@PathVariable("id") int id)

    前端会无法解析,此时要么在方法的produce属性上声明返回的是个字符串,要么就使用

    com.google.gson.Gson来完成字符串到json的转换,参考https://stackoverflow.com/questions/18385361/return-json-for-responseentitystring
    示例代码如下:
    @RequestMapping(value = "/deleteUserById/{id}", method = RequestMethod.DELETE,produces = Constant.TEXT_PLAIN)
        public ResponseEntity<String> deleteUserById(@PathVariable("id") int id)
        {
            UserDto user = userService.selectUserByID(id);
            if (user == null)
            {
                LOG.error("id为:" + id + "的用户数据不存在,无法删除");
                return new ResponseEntity<>("id为:" + id + "的用户数据不存在,无法删除",HttpStatus.NO_CONTENT);
            }
            userService.deleteUserById(id);
            LOG.info("id为:" + id + "的用户数据删除成功");
            return new ResponseEntity<>("id为:" + id + "的用户数据删除成功",HttpStatus.OK);
    //        像这样将string转成json也可以,但是要注意此时方法的produce属性也要相应的修改为json的
    //        return new ResponseEntity<>(GSON.toJson("用户数据删除成功"),HttpStatus.OK);
        }
     
  • 相关阅读:
    联想yoga pro 13 使用Hyper-v蓝屏错误PAGE_FAULT_IN_NONPAGED-AREA的解决办法
    部署 Halo 博客系统
    GiuHub高级搜索关键字
    layer提示框弹出异常抖动,而且不显示
    bootstrap同一页面上多个模态框
    border-radius
    git查看本机ssh公钥
    简单的git连接远程仓库小记
    hexo搭建完成后输入hexo s 没有生成Html静态文件
    application/xml and text/xml的区别
  • 原文地址:https://www.cnblogs.com/audi-car/p/7113439.html
Copyright © 2020-2023  润新知