SSM框架集成(一)mybatis+spring

这个demo用的是基于idea,使用maven管理项目。在idea中新建一个webapp的maven项目之后,修改pom.xml文件。我的pom文件如下(复制的时候注意,这里只贴出了properties部分):

pom.xml

  <properties>
    
    <spring.version>4.0.2.RELEASEspring.version>
    
    <mybatis.version>3.2.6mybatis.version>
    
    <slf4j.version>1.7.7slf4j.version>
    <log4j.version>1.2.17log4j.version>
  properties>
  <dependencies>
    
    <dependency>
      <groupId>commons-logginggroupId>
      <artifactId>commons-loggingartifactId>
      <version>1.1.3version>
    dependency>
    <dependency>
      <groupId>org.apache.shirogroupId>
      <artifactId>shiro-coreartifactId>
      <version>1.4.0version>
    dependency>
    
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>1.16.6version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>1.1.1version>
    dependency>
    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
      
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-oxmartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${spring.version}version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>${mybatis.version}version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.2.2version>
    dependency>
    
    <dependency>
      <groupId>javaxgroupId>
      <artifactId>javaee-apiartifactId>
      <version>8.0version>
    dependency>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.30version>
    dependency>

    
    
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>${log4j.version}version>
    dependency>


    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.1.41version>
    dependency>
    
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-apiartifactId>
      <version>${slf4j.version}version>
    dependency>

    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-log4j12artifactId>
      <version>${slf4j.version}version>
    dependency>
    
    
    <dependency>
      <groupId>org.thymeleafgroupId>
      <artifactId>thymeleafartifactId>
      <version>3.0.9.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.mybatis.generatorgroupId>
      <artifactId>mybatis-generator-coreartifactId>
      <version>1.3.5version>
    dependency>
    
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.3.1version>
    dependency>
    <dependency>
      <groupId>commons-iogroupId>
      <artifactId>commons-ioartifactId>
      <version>2.4version>
    dependency>
    <dependency>
      <groupId>commons-codecgroupId>
      <artifactId>commons-codecartifactId>
      <version>1.9version>
    dependency>
    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>${jsp-api.version}version>
    dependency>
    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>
    
    <dependency>
      <groupId>org.apache.poigroupId>
      <artifactId>poiartifactId>
      <version>3.8version>
    dependency>
    
    <dependency>
      <groupId>org.apache.poigroupId>
      <artifactId>poi-ooxmlartifactId>
      <version>3.8version>
    dependency>

    <dependency>
      <groupId>org.apache.poigroupId>
      <artifactId>poi-ooxml-schemasartifactId>
      <version>3.8version>
    dependency>
    
    <dependency>
      <groupId>org.apache.poigroupId>
      <artifactId>poi-scratchpadartifactId>
      <version>3.8version>
    dependency>
    <dependency>
      <groupId>fr.opensagres.xdocreportgroupId>
      <artifactId>org.apache.poi.xwpf.converter.coreartifactId>
      <version>1.0.6version>
    dependency>
    <dependency>
      <groupId>fr.opensagres.xdocreportgroupId>
      <artifactId>org.apache.poi.xwpf.converter.xhtmlartifactId>
      <version>1.0.6version>
    dependency>
  dependencies>

项目的目录结构如下
SSM框架集成(一)mybatis+spring_第1张图片
搭建框架基本就是在配置,在spring和mybatis集成的时候,只用配置spring的核心文件就可以了,在这里贴出我的配置

ApplicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.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.czh" />
    
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties" />
    bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        
        <property name="initialSize" value="${jdbc.initialSize}">property>
        
        <property name="maxActive" value="${jdbc.maxActive}">property>
        
        <property name="maxIdle" value="${jdbc.maxIdle}">property>
        
        <property name="minIdle" value="${jdbc.minIdle}">property>
        
        <property name="maxWait" value="${jdbc.maxWait}">property>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="mapperLocations" value="classpath:mybatis/*.xml">property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.czh.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
    bean>

    
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>
beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///android
jdbc.username=root
jdbc.password=admin
#定义初始连接数
jdbc.initialSize=0
#定义最大连接数
jdbc.maxActive=20
#定义最大空闲
jdbc.maxIdle=20
#定义最小空闲
jdbc.minIdle=1
#定义最长等待时间
jdbc.maxWait=60000

注意properties文件,行末不可以有空格,复制的时候要注意

配置好了之后,就可以写对应的实体类、Dao、service了,实体类需要符合javabean的编程规范,提供字段的get\set方法。和传统的dao不一样,在这里仅仅需要给出接口,而不需要实现,mybatis会根据dao接口和mapper.xml去隐式实现接口。

domain实体类

package com.czh.service;

import com.czh.domain.TaskList;

import java.util.List;

public interface ITaskListService {

    List list();
}

实体类需要和数据库的表对应,表里的字段,domain里面都要有,数据库要自己去建,在这里就不赘述。实体类所拥有的字段和属性是大于或等于数据库对应的表里的
mybatis有一种插件可以,写配置文件,建表之后,就可以生成实体类、dao和mapper文件。这里给出使用方法的链接 ,有兴趣的可以去了解一下mybatis generator教程
dao

package com.czh.dao;

import com.czh.domain.TaskList;

import java.util.List;

public interface ITaskListDao {

    List list();
}

这个demo采用的是注解的方式来配置spring,通常需要在dao的实现类上贴注解,但是这里的实现类是隐式生成的,无法贴注解。但是由于spring主配置的主配置文件中配置了扫描dao接口所在的包,mybatis会将dao交给spring管理。

service

package com.czh.service;

import com.czh.domain.TaskList;

import java.util.List;

public interface ITaskListService {

    List list();
}

service的实现类

package com.czh.service.impl;

import com.czh.dao.ITaskListDao;
import com.czh.domain.TaskList;
import com.czh.service.ITaskListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TaskListServiceImpl implements ITaskListService {
    @Autowired
    private ITaskListDao dao;

    public List list() {
        return dao.list();
    }
}

在实现类中千万不要忘了贴注解,还有注入dao的依赖
最后就是mybatis的映射文件了。




<mapper namespace="com.czh.dao.ITaskListDao">
    
    <resultMap id="base" type="com.czh.domain.TaskList"/>
    <parameterMap id="entity" type="com.czh.domain.TaskList"/>
    
    <select id="list" resultMap="base">
        SELECT * FROM t_tasklist
    select>
mapper>

上述配置都完成之后,别忘了去spring的主配置文件里检查一下,看看是不是都没问题。确定没问题之后,就可以跑junit测试了。
test类

import com.czh.domain.TaskInfo;
import com.czh.domain.TaskList;
import com.czh.service.ITaskInfoService;
import com.czh.service.ITaskListService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)//相当于继承那个类
//在这里配置让junit执行之前会去扫描spring主配置文件,打开spring容器
@ContextConfiguration("classpath:ApplicationContext.xml")
public class MybatisTest {
    @Autowired
    private ITaskListService service;

    @Test
    public void test1() {
        List list = service.list();
        System.out.println(list);

    }
}

你可能感兴趣的:(java框架)