【叨、校长】MyBatis+Spring整合详解教程

本教程基于Mybatis官方示例!通俗易懂、老少皆宜、不论男女!---叨、校长

废话不多说,直接上实例教程:

1、新建一个Web Project(这个大家会吧!)---我的项目名称:SSM

2、添加相应的JAR支持详情见附件(Spring包能精简,大家自己去精简,这里全部为了方便全部加入了);

3、编写实体bean,jdbc.properties文件

UserBean.java

 

package com.daodao.bean;

public class UserBean {
	public String name;
	public String pass;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
}

 jdbc.properties文件:

 

jdbc.driverClassName=com.mysql.jdbc.Driver
 
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK
 
jdbc.username=root
 
jdbc.password=abc123
 

4、配置web.xml文件,让项目支持Spring;

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">		
  <!-- 配置spring的监听器 -->
  <!-- 指明spring配置文件的位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 5、配置MyBatis配置文件--mybatis-config.xml;

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
	<!-- 给持久层对象配置个别名,能方便调用 -->
	<typeAliases>
		<typeAlias type="com.daodao.bean.UserBean" alias="User" />
	</typeAliases>
	<!-- 接口映射文件的位置 -->
	<mappers>
		<mapper resource="com/daodao/dao/IUserDao.xml"/>
	</mappers>
</configuration>

 6、写一个数据库层的接口:com.daodao.dao.IUserDao这里只有一个数据库操作的方法:getUser()

 

package com.daodao.dao;

import com.daodao.bean.UserBean;

public interface IUserDao {
	UserBean getUser(String name);
}
 

7、配置接口的映射文件IUserDao.xml;

注意:为了方便配置,务必请把此文件的名称与接口的名称一致,并且在同一路径下;

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!-- 务必配置正确namespace 就是所映射的接口类-->
<mapper namespace="com.daodao.dao.IUserDao">
	<!-- resultType="User"这个使用的就是配置文件里面的别名 -->
	<select id="getUser" parameterType="string" resultType="User">
		select * from test where name=#{name}
	</select>
</mapper>

 8、配置我们的Spring的配置文件:appliactionContext.xml

 

<?xml version="1.0" encoding="gbk"?>
<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:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!-- 获取JDBC连接属性 -->			
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<!-- 配置数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
	</bean>
    
    <!-- sqlSessionFactory -->
    <!-- MyBatis在spring中Bean的配置,都是固定的 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
    
    <!-- 配置映射器 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
      <property name="mapperInterface" value="com.daodao.dao.IUserDao" />
      <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
    </bean>
   
   <!-- 为业务逻辑层注入数据的对象 -->
    <bean id="userService" class="com.daodao.service.impl.UserServiceImpl">
    	<property name="userDao" ref="userMapper"></property>
    </bean>
    
</beans>

 8、整个项目是配置完成了,下面我进行一下测试:不知道为什么我这个能用main程序来测试,我想大概是因为sqlSessionFactory需要启动服务器才能生成吧!所以我这里使用jsp测试!直接贴代码!

 

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@page import="org.springframework.web.context.support.*,org.springframework.context.*"%>
<%@page import="com.daodao.service.impl.UserServiceImpl"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%  
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
out.print(((UserServiceImpl)wac.getBean("userService")).getUser("admin").getName());
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  <body>
  </body>
</html>

 然后大家把项目部署到服务器,启动,运行!(数据库大家自己去建立!)

关于其他详情大家去附件查看吧!想要的源代码的童鞋请留下邮箱!

 

 

你可能感兴趣的:(spring,mybatis,教程,整合)