spring MVC(十)---spring MVC整合mybatis
阿新 • • 發佈:2019-02-04
spring mvc可以通過整合hibernate來實現與資料庫的資料互動,也可以通過mybatis來實現,這篇文章是總結一下怎麼在springmvc中整合mybatis.
首先mybatis需要用到的包如圖所示:
下面是mybaits的配置檔案,寫的地方由你決定,在這裡我寫在mybatis-servlet.xml中,因為我在web.xml中設定了在tomcat啟動時會載入所有以servlet.xml結尾的檔案。
web.xml中的部分程式碼(涉及到資料來源的東西,在你的基礎上加上就行),詳細的請檢視我spring mvc系列文章的前幾篇:
mybatis-servlet.xml如下:<!-- 引用該資料來源 --> <resource-ref> <span style="white-space:pre"> </span><res-ref-name>jndi_mysql</res-ref-name> <span style="white-space:pre"> </span><res-type>javax.sql.DataSource</res-type> </resource-ref>
使用時先建立一個bean類 如Member:<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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 配置mybatis 在ioc容器中配置sqlSessionFactory 使用SqlSessionFactoryBean工廠bean 1 配置資料來源 2 配置對映檔案 注意classpath字首 每在工程中新增一個對映檔案,需要在list中新增一個value元素 --> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="ds"></property> <property name="mapperLocations"> <list> <value>classpath:<span style="color:#ff0000;">com/etock/dao/MemberDaoIf-mapper.xml</span></value> </list> </property> </bean> <!-- DataSource 1 實現類 DriverManageDataSource 2 JNDI方式 --> <!-- 第一種方式: --> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <span style="color:#ff0000;"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/cn"></property> <property name="username" value="root"></property> <property name="password" value="root"></property></span> </bean> <!-- 第二種方式: --> <!-- <bean id="ds" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/com.mysql.jdbc.Driver"></property> </bean> --> <!-- 配置介面對應的例項bean物件 spring中為了配置介面例項,提供 MapperFactoryBean的工廠bean --> <bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="ssf"></property> <property name="mapperInterface" value="<span style="color:#ff0000;">com.etock.dao.MemberDaoIf"</span>></property> </bean> <!-- 每在工程中新增一個介面,就需要在ioc容器中新增單獨的bean節點使用mapperInterface例項化改介面 --> </beans>
在定義dao層的介面:package com.etock.bean; public class Member { private Integer currentPage; private Integer pageSize; private String name; private String email; private String password; private String autograph; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAutograph() { return autograph; } public void setAutograph(String autograph) { this.autograph = autograph; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } }
package com.etock.dao;
import java.util.List;
import java.util.Map;
import com.etock.bean.Member;
public interface MemberDaoIf {
public List<Member> <span style="color:#ff0000;">selectMembersByPage</span>(Map map);
public int <span style="color:#ff0000;">selectMemberCount</span>();
}
然後是對映檔案MemberDaoIf-mapper.xml:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper <span style="color:#ff0000;">namespace="com.etock.dao.MemberDaoIf" </span>>
<!--
這裡返回的是list,但list裡面存放的還是member物件,所以還是member
-->
<select id="<span style="color:#ff0000;">selectMemberCount</span>" resultType="java.lang.Integer">
select count(*) from member;
</select>
<select id="<span style="color:#ff0000;">selectMembersByPage</span>" parameterType="java.util.Map" resultMap="<span style="color:#ff0000;">member</span>">
select * from member limit #{start},#{max};
</select>
<!--
返回型別解釋
-->
<resultMap type="com.etock.bean.Member" id="<span style="color:#ff0000;">member</span>">
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="password" column="password"/>
<result property="autograph" column="autograph"/>
</resultMap>
</mapper>
然後是controller層
package com.etock.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.etock.bean.Member;
import com.etock.dao.MemberDaoIf;
@Controller
public class MemberController {
<span style="color:#ff0000;">@Autowired
private MemberDaoIf memberDao;</span>
@RequestMapping("/selectMembersByPage")
@ResponseBody
public Map selectMembersByPage(Member member){
Integer currentPage = member.getCurrentPage();
Integer pageSize = member.getPageSize();
<span style="color:#ff0000;">int totalCount = memberDao.selectMemberCount();</span>
int totalPage = (totalCount+pageSize-1)/pageSize;
<span style="color:#ff0000;">Map map = new HashMap();
map.put("start", (currentPage-1)*pageSize);
map.put("max", pageSize);</span>
<span style="color:#ff0000;"> List<Member> list = memberDao.selectMembersByPage(map); </span>
System.out.println(list.size()+"|||"+totalCount);
/**
* responseBody 將返回值封裝成json返回給客戶端
*/
Map json = new HashMap();
json.put("list",list);
json.put("totalCount", totalCount);
json.put("totalPage", totalPage);
return json;
}
}
下面是我專案檔案的結構圖: