SpringMvc 和mybatis的集成

1.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>gbk</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
 <context-param>
 	<param-name>contextConfigLocation</param-name>
 	<param-value>classpath*:/org/sh/xml/applicationContext.xml</param-value>
 </context-param>
 <listener>
 	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <listener>
 	<listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
 </listener>
 <servlet>
 	<servlet-name>mvc-dispatcher</servlet-name>
 	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 	<load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 	<servlet-name>mvc-dispatcher</servlet-name>
 	<url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

2.配置mvc-dipatcher-servlet.xml  注意该配置文件的名称和web.xml中的相同 并且和web.xml在同一目录中

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <mvc:annotation-driven/>
    <context:component-scan base-package="org.sh.contorller"></context:component-scan>
    <mvc:default-servlet-handler/>
    <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>
配置前缀和后缀 资源文件的映射位置  注解 和自动扫描等

3.配置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:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"  
            default-autowire="byName" default-lazy-init="false"> 
	 <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 --> 
		
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
		p:url="jdbc:mysql://127.0.0.1:3306/test" 
		p:username="root" p:password="root"
		p:maxActive="10" p:maxIdle="10">
	</bean>
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	  <property name="dataSource" ref="dataSource" />
	</bean>
	
	 
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
     <!--dataSource属性指定要用到的连接池--> 
     <property name="dataSource" ref="dataSource"/> 
     <!--configLocation属性指定mybatis的核心配置文件--> 
     <property name="configLocation" value="classpath:/org/sh/xml/Configuration.xml" /> 
     <!-- 所有配置的mapper文件 -->
     <property name="mapperLocations" value="classpath*:/org/sh/xml/user.xml" />
  </bean> 
  
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 <property name="basePackage" value="org.sh.dao" />	 
  </bean>
  
	
</beans>  

4.Configuration.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>
	<typeAliases>
		<typeAlias alias="User" type="org.sh.model.User" />
		<typeAlias alias="Article" type="org.sh.model.Article" />
	</typeAliases>
	<!-- 与spring 集成之后,这些可以完全删除,数据库连接的管理交给 spring 去管理 -->
	<!--
		<environments default="development"> <environment id="development">
		<transactionManager type="JDBC"/> <dataSource type="POOLED"> <property
		name="driver" value="com.mysql.jdbc.Driver"/> <property name="url"
		value="jdbc:mysql://localhost:3306/test"/> <property name="username"
		value="root"/> <property name="password" value="root"/> </dataSource>
		</environment> </environments>
	-->
	<!--<mappers>
		<mapper resource="org/sh/xml/user.xml" />
	</mappers>
--></configuration>

5.user.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="org.sh.dao.IUserOperation">
	<select id="selectUserById" parameterType="int" resultType="User">
		select *
		from user
		where id =#{id}
	</select>
	<resultMap type="User" id="resultListUser">
		<id column="id" property="id" />
		<result column="userName" property="userName" />
		<result column="userAge" property="userAge" />
		<result column="userAddress" property="userAddress" />
	</resultMap>
	<select id="selectUsers" parameterType="String" resultMap="resultListUser">
		select *
		from user
		where userName like #{userName}
	</select>
	<!--
		执行增加操作的SQL语句。id和parameterType 分别与IUserOperation接口中的addUser方法的名字和
		参数类型一致。以#{name}的形式引用Student参数 的name属性,MyBatis将使用反射读取Student参数
		的此属性。#{name}中name大小写敏感。引用其他 的gender等属性与此一致。seGeneratedKeys设置
		为"true"表明要MyBatis获取由数据库自动生成的主 键;keyProperty="id"指定把获取到的主键值注入
		到Student的id属性
	-->
	<insert id="addUser" parameterType="User" useGeneratedKeys="true"
		keyProperty="id">
		insert
		into user(userName,userAge,userAddress)
		values(#{userName},#{userAge},#{userAddress});
	</insert>

	<!-- User 联合文章进行查询 方法之一的配置 (多对一的方式)  -->
	<resultMap type="Article" id="resultUserArticleList">
		<id property="id" column="aid" />
		<result column="title" property="title" />
		<result column="content" property="content" />
		<association property="user" javaType="User">
			<id column="id" property="id" />
			<result column="userName" property="userName" />
			<result column="userAddress" property="userAddress" />
		</association>
	</resultMap>

	<select id="getUserArticle" parameterType="int" resultMap="resultUserArticleList">
			select user.id,user.userName,user.userAddress,article.id aid,article.title,article.content
			from user,article
			where user.id = article.userid and user.id=#{id}
	</select>



</mapper>

6.编写controller
package org.sh.contorller;

import java.util.List;

import org.sh.dao.IUserOperation;
import org.sh.model.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/article")
public class UserController {
	@Autowired
	private IUserOperation userMapper;
	@RequestMapping(value="/list")
	public String listALL(Model model){
		System.out.println("======================");
		List<Article> articles = userMapper.getUserArticle(1);
		model.addAttribute("articleList",articles);
		return "article/list";
	}
}

7.在WEB-INF/jsp/article简历list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'list.jsp' starting page</title>
    

  </head>
  
  <body>
  <c:forEach items="${articleList}" var="article">
  	${article.id }--${article.title }--${article.content }<br/>
  	</c:forEach>
  </body>
</html>

8.页面显示效果:

1--test_title--test_content
2--test_title_2--test_content_2
3--test_title_3--test_content_3
4--test_title_4--test_content_4



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值