spring4 + mybatis3 + spring mvc + veloctiy + maven 整合

1,准备工作
spring4 jar 链接 https://spring.io/官网
http://maven.springframework.org/release 下载地址

mybatis3 链接 https://github.com/mybatis/mybatis-3
官网 mybatis.org 翻译的语言有很多哦,有中文的。

日志使用slf4j+logback

mysql 驱动下载 http://www.mysql.com/products/connector/

开发工具idea 下载地址http://www.jetbrains.com/idea/download/

2、通过maven命令创建 web项目
mvn archetype:create -DgroupId=com.cms -DartifactId=cms-web -DarchetypeArtifactId=maven-archetype-webapp

这里写图片描述

这里写图片描述

这里写图片描述
这里写图片描述

这里写图片描述

项目创建完成,但是项目结构还完全,需要在项目设置里面创建完成,把测试junit3 改成4
这里写图片描述

maven创建的项目web.xml 是2.5版本的
修改成3.0

<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>
</web-app>

这里写图片描述

<-- 配置web.xml 文件-->
<web-app version="3.0" 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">

    <display-name>CMS Web Application</display-name>
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <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*:spring.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>
</web-app>

spring.xml 配置文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-autowire="byName">
    <import resource="spring-web.xml"/>
    <import resource="spring-dao.xml"/>
</beans>

spring-web.xml 配置文件

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

    <context:component-scan base-package="com.cms.**" annotation-config="true"/>
    <!-- 启动spring mvc 注解 -->
    <mvc:annotation-driven/>
    <!-- 启动静态资源访问 -->
    <mvc:resources mapping="/static/**" location="/static/"/>

    <bean class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
        <property name="configLocation" value="classpath:velocity.properties"/>
    </bean>

    <bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix" value=".vm"/>
        <property name="prefix" value=""/>
        <property name="contentType" value="text/html;charset=utf-8"/>
    </bean>
</beans>

spring-dao.xml 配置文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.cms.**" annotation-config="true"/>

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- c3po 连接池配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="initialPoolSize" value="5"/>
        <property name="minPoolSize" value="2"/>
        <property name="maxPoolSize" value="30"/>
        <property name="autoCommitOnClose" value="false"/>
        <property name="acquireRetryAttempts" value="3"/>
        <property name="acquireRetryDelay" value="3000"/>
        <property name="checkoutTimeout" value="3000"/>
        <property name="maxIdleTime" value="60"/>
    </bean>

    <!-- 配置事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 使用注解事务-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加载 mybaits.cfg.xml -->
        <property name="configLocation" value="classpath:mybaits.cfg.xml"/>
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!--加载所有的mapper.xml -->
        <property name="mapperLocations" value="classpath:/mappers/*.xml"/>
        <!-- 实体别名 -->
        <property name="typeAliasesPackage" value="com.cms.domain"/>
    </bean>

    <!-- 配置扫描dao接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置dao 需要的sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 配置dao接口扫描的包名 -->
        <property name="basePackage" value="com.cms.dao"/>
    </bean>

</beans>

mybaits.cfg.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>
        <!-- JDBC 支持自动生成主键 -->
        <setting name="useGeneratedKeys" value="true"/>
        <!-- 默认为true, 使用列标签代替列名。不同的驱动在这方面会有不同的表现, 具体可参考相关驱动文档或通过测试这两种不同的模式来观察所用驱动的结果。-->
        <setting name="useColumnLabel" value="true"/>
        <!-- 是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 user_name 到经典 Java 属性名 userName 的类似映射。-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

StudentDao.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.cms.dao.StudentDao">

    <insert id="insertStudent">
        insert into student(
            student_name,
            student_age,
            student_address
        ) values(
            #{studentName},
            #{studentAge},
            #{studentAddress}
        )
    </insert>

    <update id="updateStudent">
        update student set student_name=#{studentName} where student_id=#{studentId}
    </update>

    <select id="queryAll" resultType="Student">
        select
            student_id,
            student_name,
            student_age,
            student_address,
            create_time
        from
          student
        order by create_time
        limit #{offest},#{limit}
    </select>
    <select id="queryById" resultType="Student">
        select
            student_id,
            student_name,
            student_address,
            student_age,
            create_time
        from
            student
        where
            student_id=#{studentId}
    </select>
</mapper>

dao 接口

public interface StudentDao {

    Student queryById(long studentId);

    List<Student> queryAll(@Param("offest") int offset, @Param("limit") int limit);

    int insertStudent(@Param("studentName") String studentName,
                      @Param("studentAge") int studentAge,
                      @Param("studentAddress") String studentAddress);

    int updateStudent(@Param("studentId") long studentId,
                      @Param("studentName") String studentName);
}
public interface StudentService {

    int insertStudent(String studentName, int studentAge, String studentAddress);

    int updateStudent(long studentId, String studentName);

    Student queryById(long studentId);

    List<Student> queryAll(int offset, int limit);
}
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    StudentDao studentDao;

    @Transactional
    @Override
    public int insertStudent(String studentName, int studentAge, String studentAddress) {
        return studentDao.insertStudent(studentName, studentAge, studentAddress);
    }

    @Transactional
    @Override
    public int updateStudent(long studentId, String studentName) {
        return studentDao.updateStudent(studentId, studentName);
    }

    @Override
    public Student queryById(long studentId) {
        return studentDao.queryById(studentId);
    }

    @Override
    public List<Student> queryAll(int offset, int limit) {
        return studentDao.queryAll(offset, limit);
    }
}
@Controller
public class IndexController {

    @Resource
    StudentService studentService;

    @RequestMapping("/index")
    public String index(Model model) {
        List<Student> studentList = studentService.queryAll(0, 20);
        model.addAttribute("studentList", studentList);
        return "index";
    }
}

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.cms</groupId>
    <artifactId>cms-web</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>cms-web Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>4.0.5.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>0.9.28</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.28</version>
        </dependency>

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

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

        <!-- mybatis-spring整合 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!-- serlvet  -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- veloctiy 模版 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>

        <!-- 连接池 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</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-jdbc</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-tx</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>

    </dependencies>
    <build>
        <finalName>cms-web</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <configuration>
                    <uriEncoding>UTF-8</uriEncoding>
                    <path>/</path>
                    <port>8099</port>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值