知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
<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>
<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>
<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>
創建SqlSessionFactory,需指定數據源,property名稱必須為dataSource
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean>
<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>
創建數據映射器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>
例 8.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); } }