SSM的整合

SSM整合

整合流程

 在整合spring,springMVC,mybaits时。我们先整合spring和mybatis, 然后在整合springMVC,这样的一个整合流程是一个比较方便且不容易出错的流程。那么在整合的各个阶段,什么的标志来判断我们是否已经整合成功? 在spring和mybatis整合时,我们通过编写一个简单的查询方法,并通过Junit测试这个方法,查询数据成功,则表示整合成功。 在整合springMVC时,我们编写一个可访问的地址,并且输出对应的内容,表示整合成功。

spring和mybatis的整合

  1. 引入对应的依赖
  2. 编写mapper以及对应的xml文件
  3. 编写service层代码
  4. 编写配置文件,配置数据源,事务管理器,包扫描器,sqlSessionFactory等

下面,我们来看下代码。
pom依赖:

 <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.5version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.1.12.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>5.1.12.RELEASEversion>
        dependency> 
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.1.12.RELEASEversion>
        dependency> 
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>5.1.12.RELEASEversion>
        dependency> 
          
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.8.9version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.3version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.46version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.21version>
        dependency>

mapper层代码

public interface UserMapper {
     

    public List<UserEntity> getLists();
}

mapper对应的xml代码(xml所在目录位置需要与mapper对应的包路径一样



<mapper namespace="com.sherlock.mapper.UserMapper">
    <select id="getLists" resultType="com.sherlock.entity.UserEntity">
        select * from user
    select>
mapper>

编写service层代码

public interface UserService {
     

    public List<UserEntity> getLists();
}

@Service
public class UserServiceImpl implements UserService {
     
    @Autowired
    private UserMapper userMapper;

    public List<UserEntity> getLists() {
     
        return userMapper.getLists();
    }
}

编写配置文件,这里为了区分service层的配置和dao层的配置,把配置文件分成2份
dao层配置,applicationContent-dao.xml


<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:component-scan base-package="com.sherlock.mapper">
    context:component-scan>

	
    <context:property-placeholder location="classpath*:jdbc.properties">
    context:property-placeholder>
	
	
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
        <property name="url" value="${jdbc.url}">property>
        <property name="driverClassName" value="${jdbc.driver}">property>
    bean>
	
	
    <bean id="sqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource"/>
    bean>

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

数据源配置信息

jdbc.username=root
jdbc.password=root
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.56.10:3306/common_sherlock

service层配置,applicationContext-service.xml


<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:component-scan base-package="com.sherlock.service">
    context:component-scan>
beans>

最后编写一下测试方法

@RunWith(SpringJUnit4ClassRunner.class) // 使用spring的测试类
// 配置读取配置文件位置
@ContextConfiguration(locations = {
     "classpath*:application*.xml"}) 
public class TestSpring {
     

    @Autowired
    private UserService userService;

    @Test
    public void test1() {
     
        List<UserEntity> lists = userService.getLists();

        for (UserEntity list : lists) {
     
            System.out.println(list);
        }
    }
}

springMVC和spring,mybatis的整合

  1. 引入依赖
  2. 编写一个controller
  3. 配置web.xml的监听器,以及servlet
  4. 配置springmvc配置文件,主要扫描包
  5. 浏览器访问url

pom依赖

	
	 <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webmvcartifactId>
          <version>5.1.12.RELEASEversion>
     dependency>
     <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>javax.servlet-apiartifactId>
          <version>3.1.0version>
          <scope>providedscope>
      dependency>

		
      <dependency>
          <groupId>com.fasterxml.jackson.coregroupId>
          <artifactId>jackson-coreartifactId>
          <version>2.9.0version>
      dependency>
      <dependency>
          <groupId>com.fasterxml.jackson.coregroupId>
          <artifactId>jackson-databindartifactId>
          <version>2.9.0version>
      dependency>
      <dependency>
          <groupId>com.fasterxml.jackson.coregroupId>
          <artifactId>jackson-annotationsartifactId>
          <version>2.9.0version>
      dependency>

	<build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <version>2.2version>
                <configuration>
                    <port>8081port>
                    <path>/path>
                configuration>
            plugin>
        plugins>
    build>

controller

@Controller
public class HelloController {
     

    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    @ResponseBody
    public List<UserEntity> test() {
     
        System.out.println("1111");

        return userService.getLists();
    }
}

web.xml文件

	<context-param>
		
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath*:applicationContext*.xmlparam-value>
    context-param>
    
    <listener>
    	
        <listener-class>org.springframework.web.context.ContextLoaderListener
        listener-class>
    listener>


    
    <servlet>
    	
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        servlet-class>
        <init-param>
        	
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath*:springmvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>


    <servlet-mapping>
    	
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

springmvc.xml


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

">
	
    <context:component-scan base-package="com.sherlock.controller">
    context:component-scan>

	
    <mvc:annotation-driven>mvc:annotation-driven>
beans>

浏览器访问http://localhost:8081/test

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