学习内容:学习MyBatis框架(Day57)
1、mybatis和spring整合
2、搭建ssm框架
1、mybatis和spring整合
(1)创建spring的配置文件applicationContext.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"
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.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.userName}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--引入mybatis的总配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--扫描包生成别名-->
<property name="typeAliasesPackage" value="com.hisoft.pojo"/>
<!--引入映射文件,resources目录下的mapper目录下的所有xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置mapper,使用动态代理得到实现类对象 -->
<!--<bean id="itemMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!– 配置mapper接口 (这种方式太麻烦)–>
<property name="mapperInterface" value="com.hisoft.dao.ItemMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>-->
<!--批量扫描Dao包,生成接口的实现类对象,并注入spring容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hisoft.dao"/>
</bean>
</beans>
mybatis-config.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>
<settings>
<!--使用log4j做日志管理-->
<setting name="logImpl" value="LOG4J"/>
<!--开启驼峰匹配-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置分页插件,这个拦截器插件放在配置环境的上面-->
<plugins>
<!--引入PageHelper类-->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!--数据库方言-->
<property name="dialect" value="mysql"/>
<!--设置为true时,使用rowBounds分页会进行count查询,会查询出总数-->
<property name="rowBoundsWithCount" value="true"/>
</plugin>
</plugins>
</configuration>
测试
package com.hisoft.dao;
import com.hisoft.pojo.Item;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class ItemMapperTest {
private ItemMapper itemMapper;
@Before
public void setUp() {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
itemMapper = (ItemMapper) context.getBean("itemMapper");
}
@Test
public void queryAllItems() {
List<Item> items = itemMapper.queryAllItems();
items.forEach(System.out::println);
}
}
(2)mapper整合service
service.java
@Service("itemService")
public class ItemServiceImpl implements ItemService{
@Autowired //自动注入itemMapper对象
private ItemMapper itemMapper;
@Override
public List<Item> queryItems() {
return itemMapper.queryAllItems();
}
@Override
public int insertItems(Item item) {
return itemMapper.insertItems(item);
}
}
测试
//spring的单元测试,需要添加spring-test依赖,与@RunWith(SpringJUnit4ClassRunner.class)搭配使用
//使用@ContextConfiguration注解引入配置文件,使用@Autowired自动注入bean对象
@ContextConfiguration(locations = "classpath:applicationContext.xml")
//让测试在spring容器环境下执行
@RunWith(SpringJUnit4ClassRunner.class)
public class ItemMapperTest {
@Autowired
private ItemService itemService;
/*@Before
public void setUp() {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
itemService = (ItemService) context.getBean("itemService");
}*/
@Test
public void queryAllItems() {
List<Item> items = itemService.queryItems();
items.forEach(System.out::println);
}
@Test
public void addItems(){
Item item = new Item(0,"小米",2999,"新产品");
//自动提交事务
int count = itemService.insertItems(item);
System.out.println("count = " + count);
}
}
2、搭建ssm框架
(1)springMVC配置文件mvc-servlet.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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启基于注解的bean管理 依赖注入-->
<!--自动扫描com.hisoft.controller包,数据和请求通过前端控制器到controller层再发送给服务层-->
<!--服务层返回的数据通过controller层返回到前端控制器,前端控制器将数据解析后返回给客户端-->
<context:component-scan base-package="com.hisoft.controller"/>
<!--获取配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--开启使用注解传递参数,在springMVC中只能通过注解来传递参数,不能配置文件来传递参数-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 可选属性,最大上传字节-->
<property name="maxUploadSize" value="100000"/>
</bean>
<!--访问静态资源-->
<mvc:resources mapping="/static/**" location="/static/"/>
<!--其它映射,不经过controller层直接跳转到页面,path是url路径,view-name是跳转的文件名-->
<mvc:view-controller path="/file" view-name="upload"/>
<!--异常解析器-->
<bean id="handlerExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.hisoft.exception.LoginException">redirect:/user/msg</prop>
</props>
</property>
</bean>
</beans>
controller.java
@Controller
@RequestMapping("/item/")
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("itemlist")
public String queryAll(Model model){
List<Item> items = itemService.queryItems();
model.addAttribute("items",items);
return "itemList";
}
}
itemList.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>商品列表</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>商品名称</th>
<th>价格</th>
<th>描述</th>
</tr>
<c:forEach items="${items}" var="item" varStatus="v">
<tr <c:if test="${v.count %2 eq 0}">style="background-color: aqua" </c:if> >
<td>${item.id}</td>
<td>${item.itemName}</td>
<td>${item.itemPrice}</td>
<td>${item.itemDetail}</td>
</tr>
</c:forEach>
</table>
</body>
</html>