• SpringMVC+Spring+MyBatis环境搭建


    注:不求步道于众人,抵消遗忘留足痕

    1.新建Maven工程设置字符编码为utf-8,初始化项目结构(service,controller,mapper,utils....)

    2.编写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.cbooy</groupId>
        <artifactId>xxx</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>xxx</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <!-- spring版本号 -->
            <spring.version>3.2.4.RELEASE</spring.version>
            <!-- mybatis版本号 -->
            <mybatis.version>3.2.4</mybatis.version>
            <!-- log4j日志文件管理包版本 -->
            <slf4j.version>1.6.6</slf4j.version>
            <log4j.version>1.2.9</log4j.version>
        </properties>
    
        <dependencies>
            <!-- servlet start -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <scope>provided</scope>
                <version>2.2</version>
            </dependency>
            <!-- servlet end -->
    
            <!-- junit start -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <!-- junit end -->
    
            <!-- spring start -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</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-oxm</artifactId>
                <version>${spring.version}</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.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- spring end -->
    
            <!-- spring mybatis start -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.2</version>
            </dependency>
            <!-- spring mybatis end -->
    
            <!-- mybatis start -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <!-- mybatis end -->
    
            <!-- mysql driver start -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.29</version>
            </dependency>
            <!-- mysql driver end -->
    
            <!-- ali druid from wenshao start -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.2</version>
            </dependency>
            <!-- ali druid from wenshao end -->
    
            <!-- log start -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <!-- log end -->
    
            <!-- cglib start -->
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2.2</version>
            </dependency>
            <!-- cglib end -->
    
            <!-- fast json start -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.5.4</version>
            </dependency>
            <!-- fast json end -->
    
            <!-- guava start -->
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>18.0</version>
            </dependency>
            <!-- guava end -->
        </dependencies>
    
        <build>
            <finalName>xxxx</finalName>
        </build>
    </project>
    pom

    3.配置MyBatis

      1). 在src/main/resources目录下新建目录mybatis,并新建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>
        <settings>
            <setting name="lazyLoadingEnabled" value="true" />
            <setting name="multipleResultSetsEnabled" value="true" />
            <setting name="useColumnLabel" value="true" />
            <setting name="useGeneratedKeys" value="false" />
            <setting name="defaultExecutorType" value="SIMPLE" />
            <setting name="defaultStatementTimeout" value="25000" />
        </settings>
        <mappers>
            <mapper resource="mybatis/mappers/UserMapper.xml" />
        </mappers>
    </configuration>
    configuration

      2). 在mybatis目录下新建目录mappers目录,并新建用于测试的mapper文件,UserMapper.xml

    <!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.cbooy.ms.mapper.UserMapper">
        <resultMap type="User" id="UserMap">
            <result column="id" property="id" />
            <result column="name" property="name" />
        </resultMap>
        <insert id="insertUser" parameterType="User">
            insert into t_user(id,name) values(#{id},#{name})
        </insert>
    </mapper>
    UserMapper

      3).新建接口UserMapper

    public interface UserMapper {
        void insertUser(User user);
    }
    UserMapper

      4). 新建spring配置文件dao层的配置

    <?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
        
         <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
        
        <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
            <property name="url" value="jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf-8" />
            
            <property name="username" value="root" />
            
            <property name="password" value="hao123" />
            
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            
            <property name="filters" value="stat" />
    
            <property name="maxActive" value="100" />
            
            <property name="initialSize" value="30" />
            
            <property name="maxWait" value="60000" />
            
            <property name="minIdle" value="10" />
    
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            
            <property name="minEvictableIdleTimeMillis" value="300000" />
    
            <property name="validationQuery" value="SELECT 'x'" />
            
            <property name="testWhileIdle" value="true" />
            
            <property name="testOnBorrow" value="false" />
            
            <property name="testOnReturn" value="false" />
            
            <property name="maxOpenPreparedStatements" value="20" />
            
            <property name="removeAbandoned" value="true" /> <!-- 打开removeAbandoned功能 -->
            
            <property name="removeAbandonedTimeout" value="1800" /> <!-- 1800秒,也就是30分钟 -->
            
            <property name="logAbandoned" value="true" /> <!-- 关闭abanded连接时输出错误日志 -->
        </bean>
    
        <!-- 配置 SqlSessionFactoryBean 并配置 typeAliasesPackage 指定扫描的实体类的包,自动别名 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- <property name="dataSource" ref="dataSource" /> -->
            <property name="dataSource" ref="druidDataSource" />
            <property name="configLocation" value="classpath:mybatis/configuration.xml" />
            <property name="typeAliasesPackage" value="com.cbooy.ms.model" />
        </bean>
    
        <!-- 扫描 mapper 文件 自动扫描 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.cbooy.ms.mapper" />
            <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        </bean>
    
        <!-- 配置 事务管理器 -->
        <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- <property name="dataSource" ref="dataSource" /> -->
            <property name="dataSource" ref="druidDataSource" />
        </bean>
    
        <!-- 使用声明式事务 -->
        <tx:annotation-driven transaction-manager="tx" />
    </beans>
    dao

      

    4.配置Spring

      1). 配置spring的bean文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
        <context:component-scan base-package="com.cbooy.ms">
            <context:exclude-filter type="annotation" 
                expression="org.springframework.stereotype.Controller"/>
            <context:exclude-filter type="annotation" 
                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        </context:component-scan>
    
        <import resource="classpath:springdao.xml" />
    </beans>
    beans

      2). 新建service层代码与dao层进行测试

    public interface UserService {
        void addUser(User user);
    }
    UserService
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userDao;
        
        public void addUser(User user) {
            userDao.insertUser(user);
        }
    }
    UserServiceImpl

      3). 测试在src/test目录下新建测试类 TestCases

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:springBeans.xml")
    public class TestService {
        
        @Autowired
        private UserService userService;
        
        @Test
        public void addTest(){
            userService.addUser(new User(1,"hi"));
        }
    }
    TestCases

    5.配置springmvc

      1). 配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app version="2.5" 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_2_5.xsd ">
        <display-name>Monitor Sonar</display-name>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springBeans.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>DispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>DispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <servlet>
            <servlet-name>jsp</servlet-name>
            <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>jsp</servlet-name>
            <url-pattern>*.jsp</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>false</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <welcome-file-list>
            <welcome-file>/index.html</welcome-file>
        </welcome-file-list>
    </web-app>
    web

      2). 配置springmvc的相关配置文件

    <?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:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
        <context:component-scan base-package="com.cbooy.ms" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
            <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
        </context:component-scan>
    
        <!-- 配置视图解析器 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <mvc:default-servlet-handler />
        <mvc:annotation-driven></mvc:annotation-driven>
    </beans>
    springmvc

      3).建立controller测试类UserController

    @Controller
    @RequestMapping(value = "/ms/api", method = RequestMethod.GET)
    public class UserController {
        
        @Autowired
        private UserService userService;
        
        @RequestMapping("/addUser")
        @ResponseBody
        public User addUser(@RequestParam("user") String userNa){
            
            System.out.println(userNa);
            
            User usr = new User(1,userNa);
            
            userService.addUser(usr);
            
            return usr;
        }
    }
    UserController

    至此配置部分完毕

    可能存在的问题:

      1.使用eclipse新建的maven项目中,在package explorer视图看不到src/main/java文件夹,新建时又提示文件夹已经存在等诸如此类的问题

        解决:选中项目——>右键——>Build Path——>Configure Build Path——>Source,视图下一般src/main/java 处会报错,remove掉,在新建就可以了

  • 相关阅读:
    app被Rejected 的各种原因翻译
    UIView Border color
    Centos7下安装docker
    利用Yum彻底移除docker
    Docker删除全部镜像和容器
    【转】哈哈笑一笑
    【转】KAFKA分布式消息系统
    java读取properties文件
    java反序列化php序列化的对象
    Java 1.7.0_06中String类内部实现的一些变化【转】
  • 原文地址:https://www.cnblogs.com/cbooy/p/4634378.html
Copyright © 2020-2023  润新知