Springmvc、Spring和mybatis的整合
Springmvc、Spring和mybatis的整合主要是配置文件中的更改,我们在mybatis中配置的数据库的路径和接口的路径,都交个Spring的事务来管理,其他的大致不变。
代码如下:
我们先写index.jsp页面中 的代码
代码如下:
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Mybatis+SpringMVC集成</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<!-- 用form表单提交到controller中去 -->
<form action="./user/orno">
<input type="text" name="uid"/>
<input type="submit"/>
</form>
</body>
</html>
在就是我们的controller类了
代码如下:
package com.huweiyang.Controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.huweiyang.IUDAO.UserMaperIU;
import com.huweiyang.bean.Order;
//表明这个类是controller类
@Controller
//第一层路径
@RequestMapping("/user")
publicclassUserController {
//用自动装配将接口实例化
@Autowired
UserMaperIUusermaper;
//第二层路径
@RequestMapping("/orno")
//定义一个方法来掉用接口中的方法
public ModelAndViewlistall(HttpServletRequest request,HttpServletResponse response){
// 获取jsp页面中的值,并将它转换为int类型
intid = Integer.parseInt(request.getParameter("uid"));
// 调用接口中的方法
List<Order>orders= usermaper.UserSelect(id);
// 用ModelAndView函数返回跳转到user——dorder.jsp页面中去
ModelAndViewmav=new ModelAndView("user_order");
// 通过ModelAndView用键值对的方法将值传到jsp页面中去
mav.addObject("orders",orders);
// 将这个函数返回出去
returnmav;
}
}
接着是我们的实体类:
实体类:
User:
package com.huweiyang.bean;
import java.util.List;
publicclassUser {
privateintid;
publicint getId() {
returnid;
}
publicvoid setId(intid) {
this.id = id;
}
public String getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username = username;
}
public String getMobile() {
returnmobile;
}
publicvoid setMobile(String mobile) {
this.mobile = mobile;
}
public List<Order>getOrder() {
returnorder;
}
publicvoidsetOrder(List<Order> order) {
this.order = order;
}
private String username;
private String mobile;
private List<Order> order;
}
Order:
package com.huweiyang.bean;
publicclassOrder {
privateint order_id;
privateint user_id;
private String order_no;
private String money;
private User user;
publicint getOrder_id() {
return order_id;
}
publicvoid setOrder_id(int order_id) {
this.order_id = order_id;
}
publicint getUser_id() {
return user_id;
}
publicvoid setUser_id(int user_id) {
this.user_id = user_id;
}
public String getOrder_no() {
return order_no;
}
publicvoid setOrder_no(Stringorder_no) {
this.order_no = order_no;
}
public String getMoney() {
return money;
}
publicvoid setMoney(String money){
this.money = money;
}
public User getUser() {
return user;
}
publicvoid setUser(User user) {
this.user = user;
}
}
在就是我们的接口了
代码如下:
package com.huweiyang.IUDAO;
import java.util.List;
import com.huweiyang.bean.Order;
public interface UserMaperIU {
publicList<Order> UserSelect(int id);
}
接着是我们的mybatis的实体类的配置
代码如下:
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTDMapper 3.0//EN" "mybatis-3-mapper.dtd">
<mapper namespace="com.huweiyang.IUDAO.UserMaperIU">
<resultMap type="Order" id="MapOrder">
<result property="order_id" column="order_id"/>
<result property="user_id" column="order_id"/>
<result property="order_no" column="order_no"/>
<result property="money" column="money"/>
<!-- 这里的association属性中的property的值是我们数据表中的表名,而JavaType是我们的实体类的类名 -->
<association property="user" javaType="User">
<id property="id"column="id"/>
<result property="username" column="username"/>
<result property="mobile" column="mobile"/>
</association>
</resultMap>
<!-- 编写关联两种表的sql语句 -->
<select id="UserSelect" resultMap="MapOrder" parameterType="int">
SELECTu.*,o.* FROM `user` u, `order` o WHERE u.id=o.user_id AND u.id=#{id}
</select>
</mapper>
然后是我们的mybatis的主配置:
代码如下:
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTDconfig 3.0//EN" "mybatis-3-config.dtd">
<configuration>
<!-- mybatis的主配置 -->
<typeAliases>
<!-- 表明实体类的路径 -->
<typeAlias type="com.huweiyang.bean.User" alias="User"/>
<typeAlias type="com.huweiyang.bean.Order" alias="Order"/>
</typeAliases>
</configuration>
接着是我们的Spring配置
代码如下:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName" default-lazy-init="false"
><!-- 自动装配是用什么方法 -->
<context:property-placeholder />
<!-- 配置MySQL的连接方式 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/springmv?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="lookme"/>
</bean>
<!-- 配置mybatis的主配置文件的路径 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:config/confController.xml"/>
<property name="mapperLocations" value="classpath*:com/huweiyang/IUDAO/*.xml"/>
</bean>
<!-- 配置接口路径-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.huweiyang.IUDAO"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
在是Springmvc的配置
代码如下:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 进入那个包下面的controller类 -->
<context:component-scan base-package="com.huweiyang.Controller" />
<!-- 配置前缀和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>../page/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
然后是我们的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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Mybatis_spring_springmvc_11.20</display-name>
<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>
<!-- 配置过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置spring的内置配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
最后是我们的实现功能的jsp页面
代码如下:
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<!-- 启动c标签 -->
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
<title>Insert title here</title>
</head>
<body>
<!-- 用c标签将集合中的值遍历出来,相当于for循环-->
<c:forEach items="${orders}"var="order">
订单号:${order.order_no}
<br />
单价:${order.money}
</c:forEach>
</body>
</html>
我的架构: