1. 程式人生 > >Spring Boot中引入mybatis

Spring Boot中引入mybatis

1、mybatis整合

生成資料庫表對應的Dao、Model、Mapping檔案;mybatis配置資訊,包括掃描包、JDBC屬性、資料來源等。
1.1、 生成ORMapping檔案
UserInfoMapper

package com.test.demo.mybatis.dao;

import com.test.demo.mybatis.entity.UserInfo;

public interface UserInfoMapper {
	UserInfo selectByUsername(String username);
}

UserInfo

package
com.test.demo.mybatis.entity; public class UserInfo { private Long id; private String username; private String name; private String password; private String salt; private String state; get... set... }

UserInfoMapper.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"> <mapper namespace="com.test.demo.mybatis.dao.UserInfoMapper"> <resultMap id="BaseResultMap" type="com.test.demo.mybatis.entity.UserInfo"> <id column="ID" jdbcType="DECIMAL" property="id" /> <result column=
"USERNAME" jdbcType="VARCHAR" property="username" /> <result column="NAME" jdbcType="VARCHAR" property="name" /> <result column="PASSWORD" jdbcType="VARCHAR" property="password" /> <result column="SALT" jdbcType="VARCHAR" property="salt" /> <result column="STATE" jdbcType="VARCHAR" property="state" /> </resultMap> <sql id="Base_Column_List"> ID, USERNAME, NAME, PASSWORD, SALT, STATE </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from ORCL.USER_INFO where ID = #{id,jdbcType=DECIMAL} </select> <select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from ORCL.USER_INFO where username = #{id,jdbcType=VARCHAR} </select> </mapper>

1.2、 mybatis配置
pom.xml

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<!-- created by mvn install command -->
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc7</artifactId>
			<version>12.1.0</version>
		</dependency>

application.properties

mybatis.mapper-locations=classpath*:com/test/demo/mybatis/mapper/*.xml

spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@10.xx.xx.37:1521:xe
spring.datasource.username=orcle1
spring.datasource.password=orcle1

DemoApplication

@SpringBootApplication
// 在啟動類中新增對mapper包掃描@MapperScan
@MapperScan({"com.test.demo.mybatis.dao"})
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

新增mybatis-config.xml

<configuration>
	<typeAliases>
		<typeAlias alias="Integer" type="java.lang.Integer" />
		<typeAlias alias="Long" type="java.lang.Long" />
		<typeAlias alias="HashMap" type="java.util.HashMap" />
		<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
		<typeAlias alias="ArrayList" type="java.util.ArrayList" />
		<typeAlias alias="LinkedList" type="java.util.LinkedList" />
	</typeAliases>
</configurati

2、業務模擬

2.1、 controller
HelloController中增加方法。

	@Resource
	UserInfoService service;
	
	@RequestMapping("/getUserByName")
	public UserInfo getDmCxbb(UserInfo key) {
		return this.service.findByUsername(key.getUsername());
	}

2.2、 Service層
UserInfoService介面

package com.test.demo.mybatis.service;
import com.test.demo.mybatis.entity.UserInfo;
public interface UserInfoService {
	/** 通過username查詢使用者資訊 */
	public UserInfo findByUsername(String username);
}

UserInfoServiceImpl實現類

@Service
public class UserInfoServiceImpl implements UserInfoService {
	@Resource
	UserInfoMapper userDao;

	@Override
	public UserInfo findByUsername(String username) {
		return userDao.selectByUsername(username);
	}
}

3、執行

啟動DemoApplication後,在瀏覽器中訪問如下地址:
http: //cos6743:8081/getUserByName?username=yangtom
返回json:
{“id”:802,“username”:“yangtom”,“name”:“10001”,“password”:“1111153885f51222d0b”,“salt”:null,“state”:null}