Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

11.5. 整合 Mybatis

11.5.1. pom.xml

			
 		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.3.0</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.3</version>
		</dependency>
   			
			

11.5.2. properties

			
 	<bean id="configuracion"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:resources/development.properties" />
	</bean>
   			
			
			
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.4.9:1521:orcl
#jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.username=test
jdbc.password=123456
   			
			
			
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>			
			
			

11.5.3. dataSource

			
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
	</bean>
			
			

11.5.4. SqlSessionFactory

創建SqlSessionFactory,需指定數據源,property名稱必須為dataSource

			
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
	</bean>
			
			

11.5.5. Mapper 掃瞄

			
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.netkiller.mappers" />
		<property name="annotationClass" value="cn.netkiller.mappers.annotation.MybatisMapper"/>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	</bean>
			
			

11.5.6. Mapper 單一class映射

創建數據映射器Mapper,屬性mapperInterface的value必須為介面類

			
	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
		<property name="mapperInterface" value="com.mybatis.demo.UserMapper" />  
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />  
	</bean>
			
			

11.5.7. Service

			
	<bean id="userService" class="cn.netkiller.service.UserService">
	</bean>
			
			

11.5.8. 測試實例

例 11.1. MyBatis

建立映射

				
package cn.netkiller.mapper;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import cn.netkiller.model.User;

public interface UserMapper {
	@Select("SELECT * FROM `user` WHERE id = #{id}")
	public User findById(@Param("id") int id); 
}
				
				

建立模型

				
package cn.netkiller.model;

public class User {
	private String id;
	private String name;
	private int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

				
				

建立 service

				
package cn.netkiller.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.netkiller.mapper.UserMapper;
import cn.netkiller.model.User;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;

	public UserMapper getUserMapper() {
		return userMapper;
	}

	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	public User findById(int id) {
        return userMapper.findById(id);
    }
}
				
				
				

建立控製器

				
package cn.netkiller.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import cn.netkiller.mapper.UserMapper;
import cn.netkiller.model.User;
import cn.netkiller.service.UserService;

@Controller
public class Index {

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
    private UserService userService;
	
	
	@RequestMapping("/index")
	// @ResponseBody
	public ModelAndView index() {

		String message = "Hello";
		return new ModelAndView("index/index", "variable", message);
	}

	@RequestMapping("/user")
	public ModelAndView user() {
		
		User user = userService.findById(2);
		String message = user.toString();
		return new ModelAndView("index/index", "variable", message);
	}
	
	@RequestMapping("/member")
	public ModelAndView member() {
		User user = userMapper.findById(2);
		String message = user.toString();
		return new ModelAndView("index/index", "variable", message);
	}
}