SpringMVC整合mybatis

使用Spring+SpringMVC+Mybatis实现SSM开发方式

1、创建一个web工程

2、导入所有需要使用到的jar包

 1.Mybatis需要的所有jar包。

 2.Spring的所有jar包。

 3.数据库驱动包。

 4.Mybatis和Spring的整合包。

 5.log4j的日志包。

 6.dbcp数据库连接池的jar包。

 7.jstl的jar包。

3、整合思路

 1.整合dao层
  mybatis和spring进行整合,通过spring来管理mapper接口。使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
 2.整合service层
  通过sprin来管理service接口。使用配置方式将service接口配置到spring的配置文件中。实现事务的控制。
 3.整合springmvc
  因为springmvc是spring的一个模块,不用进行中间层整合。

4、创建mybatis的全局配置文件mybatis.xml

 由于使用了Spring来管理mybatis,所有在mybatis的全局文件中只需要配置pojo的别名和mybatis的全局配置参数。







5、创建Spring的配置文件

 在Spring的配置文件中要配置数据源,SqlSessionFactory和mapper扫描器。



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






















6、编写POJO

 pojo中的属性名称要和数据库中列名称保持一致。

public class Items {
private Integer id;

private String name;

private Float price;

private String pic;

private Date createtime;

private String detail;
//setter and getter
}

7、编写ItemMapper.xml文件

 第六步和第七步可以使用mybatis的逆向工程进行代码生成,具体方法见 mybatis逆向工程






8、编写ItemMapper接口文件

public interface ItemsMapper {

public List queryItesm() throws Exception;

}

9、编写service接口文件

public interface ItemsService {
public List queryItesm() throws Exception;

}

10、编写service接口的实现

public class ItemsServiceImpl implements ItemsService {

//注入mapper
@Autowired
ItemsMapper itemsMapper;

@Override
public List queryItesm() throws Exception {
return itemsMapper.queryItesm();
}

}

11、编写Spring的配置文件来配置service




12、编写Spring的事务控制配置文件






<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:method name="get
" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>

aop:config
<aop:advisor advice-ref="txAdvice" pointcut="execution(
com.jack.service.impl..(..))"/>
</aop:config>

13、编写SpringMVC的配置文件

mvc:annotation-driven</mvc:annotation-driven>





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








14、编写handler

@Controller
public class controller {

//注入service接口
@Autowired
ItemsService itemsService;
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{
List itemsExtendList =itemsService.queryItesm();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsExtendList",itemsExtendList);
modelAndView.setViewName("itemlist");
return modelAndView;
}
}

15、编写view

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>



商品展示












<c:forEach items="${itemsExtendList}" var="items">





</c:forEach>
商品id 商品名称 商品价格
${items.id} ${items.name} ${items.price}



16、编写web.xml文件




contextConfigLocation
classpath:config/applicationContext-*.xml



org.springframework.web.context.ContextLoaderListener



springmvc
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:config/springmvc.xml


springmvc

*.action

17、数据库的配置文件

mysql.jdbc.driver = com.mysql.jdbc.Driver
mysql.jdbc.url = jdbc:mysql:///springmvc
mysql.jdbc.username= root
mysql.jdbc.password = root

18、log4j的配置文件

Global logging configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug

log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

posted @ 2017-08-14 14:15  一条路上的咸鱼  阅读(358)  评论(0)    收藏  举报