maven+spring+spring+myBatis整合

目录:

    • 一 开发环境
    • 二 开发步骤
    • 三 xml AND pom
    • 四 编码
    • 测试

一 、开发环境

1. jdk 1.8
2. eclipse neon2
2. Maven 3.3.9
3. MySQL 5.5.49
4. spring 、 springmvc 4.2.4RELEASE
5. myBatis 3.2.8 (mybatis-spring 1.2.2)

二 、开发步骤

    1.创建 maven 的 web 项目,可以建立一个web项目,也可以创建一个父工程
    2.创建 properties 配置文件
    3.mybatis + spring + springmvc 配置文件 (这里指定包的时候就可以创建出来)
    4.配置 web.xml
    5.编码
    6.测试

三 、xml AND pom

1. SqlMapConfig.xml(给个表头什么都不用写 - 这里我们在 src/main/resources 创建 mybatis 文件夹)       


<configuration>configuration>
2.applicationContext_dao.xml(持久层,src/main/resources 创建 spring 文件夹)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">
    
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driverClass}" />
        <property name="url" value="${jdbcUrl}" />
        <property name="username" value="${user}" />
        <property name="password" value="${password}" />
    bean>

    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
        <property name="typeAliasesPackage" value="com.ssm.pojo">property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssm.dao" />
    bean>
beans>

### db.properties  (数据库配置信息)
    driverClass=com.mysql.jdbc.Driver
    jdbcUrl=jdbc:mysql://127.0.0.1:3306/springmvc
    user=username
    password=password
3. applicatoinContext_service.xml(src/main/resources/spring)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
    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.ssm.service" />

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
beans>
4. springmvc.xml(src/main/resources/spring)

<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/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.ssm.controller" />
    
    <mvc:annotation-driven />
    
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
    bean>
beans>
5. web.xml

<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_2_5.xsd"
    id="WebApp_ID" version="2.5">

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring/applicationContext_*.xmlparam-value>
    context-param>
     
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    <servlet>
        <servlet-name>crmservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            
            <param-value>classpath:spring/springmvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>

    <servlet-mapping>
        <servlet-name>crmservlet-name>
        
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>

    
    <filter>
        <filter-name>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
web-app>
6. pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.ssmgroupId>
    <artifactId>ssmartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>2.3.2version>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <version>2.2version>
                <configuration>
                    <port>8080port>
                    <path>/ssmpath>
                configuration>
            plugin>
        plugins>
    build>
    <dependencies>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
            <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-messagingartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jmsartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-expressionartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>4.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.8version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.10version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.2.2version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.9version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
        dependency>
        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        <dependency>
          <groupId>org.apache.taglibsgroupId>
          <artifactId>taglibs-standardartifactId>
          <version>1.2.1version>
          <type>pomtype>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>3.2version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>2.4.0version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>2.4.4version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.4.4version>
        dependency>
    dependencies>
project>

这里使用逆向工程生成mapper , pojo
我使用的逆向工程下载链接 :逆向工程
这里mapper.xml 的配置文件 ,我是放在 src/main/resources 下面创建的跟 src/main/java 同样的层级目录放置


四 、 编码

既然你看到了这里,想必你已经了解过 maven , spring , pringmvc ,mybatis , 整合重点就是配置文件
1. Controller

@Controller
@RequestMapping("/XXX") //注解指定请求的url
public class AaaController {

    @Autowired
    private AbbService abbService;
    //返回    ModelAndView 方式
    @RequestMapping("/yyy.action")//注解指定请求的url
    public String xxx(Model model,Pojo pojo)  {
        //调用service代码处理

        //参数回显
        model.addAttribute("回显参数","service请求回来的参数");
        return "页面名字(没有.jsp)";
    }
}
2. Service
public interface AbbService {//方法}

@Service
@Transactional
public class AbbServiceImpl implements AbbService{
    @Autowired
    private AbcDao abcDao;
    //方法逻辑处理
}
3.Dao
public interface AbcDao{
    //方法 - 这里注意方法名和mapper.xml id 一致!
}

测试

1. 单独测试 
    您可以在serivce 自己模拟数据 , 用 @Test 测试您的持久层
2. 整合测试
    想必一个简单的dmeo 你已经写出来了。你可以把项目跑起来测试
    安装工程:右键工程——Run AS——Maven install。
    运行:右键工程——Run AS——build..   Goals 输入 clean tomcat7:run //这里是配置一起启动命令,后期可能还需要运行,配置一个clean 是清除

你可能感兴趣的:(springmvc,spring,mybatis/ibatis)