Tags: From zero development projects


Use Maven to set up the SSM environment

What are the basic JAR packages required for SSM? Tidy it up:

  • C3p0 data connection pool
  • [x] for springMVC JSON
  • springAOP
  • springCore
  • springPersitence
  • springWeb
  • myBatis
  • myBatisSpring
  • mysql
  • Mybatis reverse engineering plugin

2017/10/24 16:00:31

I went to mvnrepository.com/ to find jars. Most use the latest jars

  • Spring5 was originally used for integration, but it conflicted with asm and there were no online solutions.
  • The compromise was made to 3.2.2

Pom.xml file configuration

pom.xml

<?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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>

    <name>fuchengWeb</name>
    <groupId>fuchengWeb</groupId>
    <artifactId>fuchengWeb</artifactId>
    <version>1.0 the SNAPSHOT</version>
    
    <! -- Reverse engineering plugin -->
    <build>
        <finalName>zhongfucheng</finalName>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>


        <! --c3p0-->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <! --json-->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>


        <! --mybatis-spring-->

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <! --mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>

        <! --spring aop-->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.11</version>
        </dependency>

        <dependency>
            <groupId>org.aopalliance</groupId>
            <artifactId>com.springsource.org.aopalliance</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.cglib</groupId>
            <artifactId>com.springsource.net.sf.cglib</artifactId>
            <version>2.1.3</version>
        </dependency>

        <! --spring-core-->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>com.springsource.org.apache.commons.logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.2. RELEASE</version>
            <scope>test</scope>
        </dependency>


        <! --spring-persitence-->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.transaction</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>

        <! --spring-web-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.2. RELEASE</version>
        </dependency>

        <! --mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.22.0 - CR2</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.2.5</version>
        </dependency>

        <dependency>
            <groupId>org.objectweb.asm</groupId>
            <artifactId>com.springsource.org.objectweb.asm</artifactId>
            <version>2.2.3</version>
        </dependency>
    </dependencies>


</project>


Copy the code

If the download is too slow, we can use ali Cloud private server. Just configure the following code under Settings:


<mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>

Copy the code

Create mysql user and database

Mysql user:

  • User name: Zhongfucheng
  • Password: zhong

Database:

  • zhongfucheng

Mybatis configuration file

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>
    <! -- Simplify the use of classes with aliases -->
    <typeAliases>
        <package name="zhongfucheng.entity"/>
    </typeAliases>
</configuration>

Copy the code

Application-context configuration file


<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <! -- Configure data source -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl"
                  value="jdbc:mysql://localhost:3306/zhongfucheng? useUnicode=true& characterEncoding=UTF-8"/>
        <property name="user" value="zhongfucheng"/>
        <property name="password" value="zhong"/>
    </bean>

    <! Mybatis configuration file -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:myBatis-config.xml"/>
        <! Mybatis config file is not required to load SQL mapping files.
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>

    </bean>

    <! Configure transaction manager to manage data source transaction processing -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <! -- Configure transaction notification -->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <! Background-for ="Exception/Throwable" -->
            <tx:method name="insert*" propagation="REQUIRED"
                       rollback-for="Exception"/>
            <tx:method name="update*" propagation="REQUIRED"
                       rollback-for="Exception"/>
            <tx:method name="delete*" propagation="REQUIRED"
                       rollback-for="Exception"/>
            <tx:method name="*" propagation="SUPPORTS"/>
        </tx:attributes>
    </tx:advice>
    <! Set transaction boundaries at service layer -->
    <aop:config>
        <aop:advisor advice-ref="advice"
                     pointcut="execution(* zhongfucheng.service.impl.*.*(..) )"/>
    </aop:config>

    <! The SessionTemplate has been configured to encapsulate the tedious data operations, so that the transaction does not need to be committed. -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

    <! - Automatically scan components to remove controllers, which are configured in spring-mVC.xml. If not removed, transaction management will be affected. -->
    <context:component-scan base-package="zhongfucheng">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <! -- Use Mapper proxy -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="zhongfucheng.dao" />
    </bean>
</beans>
Copy the code

Configure the spring MVC

spring-mvc.xml


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

    <! Enable json format support -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <! -- Scan all controllers but not service -->
    <context:component-scan base-package="zhongfucheng">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Service" />
    </context:component-scan>

    <! -- View resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>


</beans>
Copy the code

A configuration file generated in reverse order

generatorConfig.xml



<?xml version="1.0" encoding="UTF-8" ? >

      
<generatorConfiguration>
    <! Mysql > create driver package path
    <classPathEntry
            location="X: \ Users \ ozc \ Desktop \ zhongfuchengWeb/mysql/mysql - connector - Java \ 5.1.34 \ mysql connector - Java - 5.1.34. Jar"/>
    <! Configure where the data source and generated code are stored
    <context id="context1">
        <commentGenerator>
            <! Uncomment -->
            <property name="suppressAllComments" value="true"/>
            <! Generate timestamp -->
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/zhongfucheng" userId="zhongfucheng"
                        password="zhong"/>
        <! -- The default resource pack for the generated entity class SRC -->
        <javaModelGenerator targetPackage="zhongfucheng.entity" targetProject="src/main/java"/>
        <! SqlMap file location, default resource pack SRC -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
        <! -- Configure dao interface to generate information -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="zhongfucheng.dao" targetProject="src/main/java"/>
        
        <! TableName: tableName schema: do not fill in -->
        <table schema="" tableName="EB_ORDER_DETAIL" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false"
        >
        </table>
    </context>
</generatorConfiguration>

Copy the code

The web.xml configuration

web.xml


<?xml version="1.0" encoding="UTF-8"? >
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <! --Spring listener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicatoin-context.xml</param-value>
    </context-param>

    <! --springMVC core allocator -->
    <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-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <! -- Chinese garble filter -->
    <filter>
        <filter-name>SpringCharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SpringCharacterEncodingFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
</web-app>
        
Copy the code

Log4j configuration file

log4j.properties

# Rules reminder:
# DEBUG < INFO < WARN < ERROR < FATAL

# Global logging configuration
log4j.rootLogger=debug,stdout

# My logging configuration...
log4j.logger.cn.jbit.mybatisdemo=DEBUG


## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

log4j.logger.org.apache.ibatis=DEBUG
## log4j.logger.org.apache.jdbc.SimpleDataSource=DEBUG
log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=DEBUG
## log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapclientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

Copy the code

Test reverse engineering

My mysql driver package is too high, there is the following problem:

	The server time zone value '� й � � � ׼ ʱ � �' is unrecognized or represents more than one time zone.
Copy the code

So I went and found a solution, just add the time zone after the URL:


jdbc:mysql://localhost:3306/zhongfucheng? serverTimezone=UTC
Copy the code

Then came the following question:


Column name pattern can not be NULL or empty
Copy the code

The solution found is to add arguments to the end again:


jdbc:mysql://localhost:3306/zhongfucheng? serverTimezone=UTC& useSSL=false& nullNamePatternMatchesAll=true

Copy the code

And it worked…

. Another problem: I didn’t notice at first that mapper generated by Mybatis Generator only has insert methods

Link: http://blog.csdn.net/qq_15127715/article/details/78139228

Went to check a lot of information, found or because mysql driver package is too high problem. We have to go down.

Failed to read candidate Component class

Spring 3.2 does not support a 1.8 build environment, so the solution is to switch to a 1.7 build environment

Connection: http://www.cnblogs.com/cxj20160928/p/5954336.html

Development Environment Summary

  • Intellij idea
  • Spring 3.2.2
  • Mybatis3.4.5
  • Mysql 5.1.34
  • Tomcat 7.0
  • jdk1.7

Spent a night summed up: don’t be too forced to play the new JAR package, the information is too little, very prone to error! # #

There was a lot of trouble trying to use the new version of the JAR package

  • Tomcat cannot be started because spring5.0 conflicts with asm, reduced to 3.2.2
  • Mysql 6.0 reverse engineering in the time zone, can not create table problems, after a round of solution, finally generated SqlMapper only insert statement. It then fell to 5.1.34
  • There was an out-of-date error when using JDK1.8, which was later resolved by adding a configuration in POM.
  • Objects cannot be created when the Spring container is initialized. Spring3.2.2 conflicts with JDK1.8. It then dropped to JDK1.7
    • There are four sections that need to be specified to drop the JDK version in Intellij Idea, see the connection above for details.

If you find this article helpful, give the author a little encouragement