mybatis与Spring框架整合

首先,两个框架要整合到一起的话,需要配置好各自所需要的jar包
在这里插入图片描述
但实际上可能不需要这么多jar包,比如接下来的例子里aop,aspects,就不会用到。
实际上,我们可以首先搭建一个mybatis环境,然后再进行修改。
大体结构如下:
在这里插入图片描述
首先web.xml里配置Spring:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssm</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>DispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:app.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>DispatcherServlet</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

当两个框架整合后mybatis-config.xml中environments标签将不再起作用,将不再使用mybatis-config.xml全局配置文件中数据库配置。此时在app.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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" value="#{dataSource}"></property>
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<property name="mapperLocations" value="classpath:sql/*.xml"/>
	</bean>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<context:component-scan base-package="com.zzu"></context:component-scan>
	<mybatis-spring:scan base-package="com.zzu"/>
</beans>

我们可以看到,第一个bean配置的是数据库连接池,第二个bean则是配置两个框架整合的桥梁----SqlSessionFactoryBean类。第三个bean是springMvc的配置,配置controller类里方法String类型返回值的拼接来返回一个页面。

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

众所周知,该配置能为@controller,@component,@repository注解修饰的类自动创建对象。

sql包下放置的依然是sql相关语句

<?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="com.zzu.student.IStudentInfo">
	<select id="selectName" resultType="java.lang.String">
		select name from area where code = #{code}
	</select>
</mapper>

此时的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>
		
</configuration>

userInfoController类:

package com.zzu.userinfo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/userinfo")
public class UserInfoController {
	@RequestMapping("/login.do")
	public String login() {
		System.out.println(111);
		return "login";
	}
}

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
	location.href="userinfo/login.do"
</script>

logn.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title></title>
	</head>
	<body>
		登陆
	</body>
</html>

该例子就是一个很简单的mybatis与Spring框架整合的例子。效果就是当访问
127.0.0.1/ssm 时先访问了index.jsp,然后瞬间访问了controller,然后又返回一个页面

发布了74 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/naruhina/article/details/97044335
今日推荐