Spring整合MyBatis

时间:2020-12-05 04:32:30

Spring整合MyBatis

前言:MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

想瞭解更多可訪問:http://code.google.com/p/mybatis/

我的開發環境
  框架: springmvc+spring+freemarker+mybatis
  開發工具: springsource-tool-suite-2.9.0
  JDK版本: 1.6.0_29
  tomcat版本:apache-tomcat-7.0.26

本文地址:http://www.cnblogs.com/sunang/p/3431596.html  轉載請注明出處^_^

要注意的點已经用          標注,請大家要特別注意。

前置文章

  SpringMVC配置入門    http://www.cnblogs.com/sunang/p/3419544.html

  Spring整合Freemarker  http://www.cnblogs.com/sunang/p/3419676.html

now go!

step1.引入jar包

Spring整合MyBatis

Maven代碼如下:

        <!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
<!-- @Resource注解所需依賴 -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
<!-- JDBC连接数据库所需依赖 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>com.springsource.org.apache.commons.dbcp</artifactId>
<version>1.2.2.osgi</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>

step2.編輯spring配置文件,代碼如下:

    <!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="mySqlDataSource" />
<!-- 配置sql語句映射文件路徑 -->
<property name="mapperLocations" value="classpath:conf/mapper/*Mapper.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- 配置数据源 -->
<bean id="mySqlDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/sample"></property>
<property name="username" value="root"></property>
<property name="password" value="12345678"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>

step3.在step.2配置好的src/main/resources/conf/mapper目錄下,編輯sql映射文件UserDaoMapper.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">
<!-- 在DAO中將使用命名空間(namespace)+id的方式來定位sql語句-->
<mapper namespace="user">
<select id="queryUserCount" resultType="int"><!-- resultType的值為返回的結果類型 -->
select count(*) from user;
</select>
</mapper>

step4.編輯Dao、Controller、視圖文件,代碼分別如下:

DAO接口UserDao.java:

package www.asuan.com.dao;

public interface UserDao {
int queryUserCount();
}

DAO實現UserDaoImpl.java:

package www.asuan.com.dao.impl;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository; import www.asuan.com.dao.UserDao; @Repository("userDao")
public class UserDaoImpl implements UserDao { @Resource
private SqlSession sqlSession;// 注入sqlSession public int queryUserCount() {
// 使用sqlSession進行操作
return sqlSession.selectOne("user.queryUserCount");
}
}

controller:

package www.asuan.com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import www.asuan.com.dao.UserDao; @Controller
@RequestMapping("/learnMVC")
public class SpringMybatisController { @Autowired
UserDao userDao; @RequestMapping("/getUserCount")
public String getUserCount(Model model) {
int userCount = userDao.queryUserCount();//調用DAO方法獲得數據
model.addAttribute("userCount", userCount);
return "userCount.ftl";
}
}

視圖文件userCount.ftl:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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>Insert title here</title>
</head>
<body>
user表中總共有:---${userCount}---條數據
</body>
</html>

step.5 數據庫準備

本文以mysql作為數據庫,在sample下新建user表,數據如下:

Spring整合MyBatis

step.6 運行與測試

部署并運行工程,瀏覽器訪問:http://localhost:8080/你的工程名/learnMVC/getUserCount.htm

運行結果:

Spring整合MyBatis

complete!