mybatis+spring+springmvc整合教程

来源:互联网 发布:淘宝排名靠前技巧软件 编辑:程序博客网 时间:2024/05/21 19:31

整合思路:


第一步:整合dao

mybatisspring整合,通过spring管理mapper接口。

使用mapper的扫描器自动扫描mapper接口在spring中进行注册。

 

第二步:整合service

通过spring管理service接口。

使用配置方式将service接口配置在spring配置文件中。

实现事务控制。

 

第三步:整合springmvc

由于springmvcspring的模块,不需要整合。


开始框架的搭建:

1.所需要的jar包



工程结构:


2.在config/mybatis下面创建SqlMapConfig.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>
<!-- 配置setting全局,根据需要添加 -->
<!-- 配置别名 -->
<typeAliases>
<package name="cn.itcast.ssm.po"/>
</typeAliases>

<!-- mapper不用配置 ,使用spring和mybatis的整合包进行mapper的扫描,必须遵循一定的规则-->
<!-- <mappers>
<mapper resource="sqlmap/User.xml"/>
<package name="cn.itcast.ssm.mapper"/>
</mappers>
 -->
</configuration>

3.在config下面spring的包中,spring/applicationContext-dao.xml

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


<!-- 配置数据源,dbcp -->
<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 ,dbcp -->

<!--<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="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean> -->

<!-- 使用c3p0来进行数据库的链接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="5" />
</bean>
<!-- sqlSessionFacotry -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 使用mapper接口的扫描器,自动生成mapper接口的实例化对象,对象为接口名称的小写,其作用和下面相似,能够自动扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!--如果有多个,用逗号隔开-->
<property name="basePackage" value="cn.itcast.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

<!-- 使用MapperFactoryBean能将mapper接口实例化,不用再生成类和对象,直接调用实例化的接口对象就能够调用方法。 -->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

<property name="mapperInterface" value="cn.itcast.ssm.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> -->
</beans>

4.逆向工程生成如下:



5.自定义mapper,

 ItemsMapperCustom.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="cn.itcast.ssm.mapper.ItemsMapperCustom" >


   <!-- 定义商品查询的sql片段,就是商品查询条件 -->
   <sql id="query_items_where">
    <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
    <!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 -->
    <if test="itemsCustom!=null">
    <if test="itemsCustom.name!=null and itemsCustom.name!=''">
    items.name LIKE '%${itemsCustom.name}%'
    </if>
    </if>
   </sql>
 
  <!-- 商品列表查询 -->
  <!-- parameterType传入包装对象(包装了查询条件)
  resultType建议使用扩展对象
  -->
  <select id="findItemsList" parameterType="cn.itcast.ssm.po.ItemsQueryVo"
  resultType="cn.itcast.ssm.po.ItemsCustom">
  SELECT items.* FROM items  
  <where>
  <include refid="query_items_where"></include>
  </where>
  </select>

</mapper>

 ItemsMapperCustom.java

public interface ItemsMapperCustom {
    
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}

6.整合service

建立文件结构


public interface ItemsService {
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}


public class ItemsServiceImpl implements ItemsService{
//自动注入
@Autowired
private ItemsMapperCustom itemsMapperCustom;
@Override
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
List<ItemsCustom> itemsCustom = itemsMapperCustom.findItemsList(itemsQueryVo);
return itemsCustom;
}
}

7.在config下面spring中配置applicationContext-service.xml

<bean id="itemsService" class="cn.itcast.ssm.service.impl.ItemsServiceImpl"></bean>

8.在config下面spring中配置applicationContext-transaction.xml

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txadvice" pointcut="execution(* cn.itcast.ssm.service.impl.*.*(..))"/>
</aop:config>

9.整合springmvc,在config/spring文件下创建springmvc.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd 
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--主键扫描,可以扫描controller,这里指定controller的包 -->
<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>

<!--适配器,映射器的自动扫描-->
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

10.配前端控制器web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>springmvcfirst</display-name>
  
  <!-- 配置前端控制器 -->
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- contextConfigLocation 配置springmvc加载的配置文件-->
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springmvc.xml</param-value>
  </init-param>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <!-- 
  第一种:*.action,访问以.action结尾由DispatcherServlet进行解析
  第二种:/,所有的访问地址都有DispatcherServlet进行解析,对于静态的文件的解析需要进行不让DispatcherServlet解析
  第三种:/*,这样的配置不对,使用这种配。
  -->
 
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
11.编写controller
@Controller
public class ItemsController {


@Autowired
private ItemsMapperCustom itemsMapperCustom;
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{

List<ItemsCustom> itemsList = itemsMapperCustom.findItemsList(null);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
modelAndView.setViewName("items/itemsList");

return modelAndView;

}
}
12.最后加载spring的容器,在web.xml最上端添加
<!--加载spring的容器  -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>








0 0
原创粉丝点击