SpringBoot深度整合Mybatis

1.我們需要加入通用Mapper和分頁插件,所以需要在pom.xml加入以下依賴

	<!-- 通用Mapper -->
		<dependency>
			<groupId>com.github.abel533</groupId>
			<artifactId>mapper</artifactId>
			<version>2.3.4</version>
		</dependency>
		<!-- 分頁助手 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>3.7.5</version>
		</dependency>
		<dependency>
			<groupId>com.github.jsqlparser</groupId>
			<artifactId>jsqlparser</artifactId>
			<version>0.9.1</version>
		</dependency>

2.修改配置文件

#spring集成Mybatis環境
#pojo別名掃描包
mybatis.type-aliases-package=cn.itcast.springboot.pojo
#加載Mybatis核心配置文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.config-location=classpath:mybatis/SqlMapConfig.xml
#配置連接池,還需要在pom.xml中加入該連接池的依賴
#spring.datasource.type=com.jolbox.bonecp.BoneCPDataSource

3.在src\main\resources\mapper路徑下加入UserMapper.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="cn.it.springboot.mapper.UserMapper">
	<select id="queryAll" resultType="user">
		select * from user
	</select>
</mapper>

4.在src\main\resources\mybatis加入SqlMapConfig.xml配置文件,用以加載通用Mapper和分頁助手

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 分頁助手 -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<property name="dialect" value="mysql" />
			<!-- 該參數默認爲false -->
			<!-- 設置爲true時,使用RowBounds分頁會進行count查詢 -->
			<property name="rowBoundsWithCount" value="true" />
		</plugin>

		<!-- 通用Mapper -->
		<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
			<!--主鍵自增回寫方法,默認值MYSQL,詳細說明請看文檔 -->
			<property name="IDENTITY" value="MYSQL" />
			<!--通用Mapper接口,多個通用接口用逗號隔開 -->
			<property name="mappers" value="com.github.abel533.mapper.Mapper" />
		</plugin>
	</plugins>
</configuration>

5.編寫Mapper


//extends com.github.abel533.mapper.Mapper<User>:需要繼承通用Mapper
@Mapper
public interface UserMapper extends com.github.abel533.mapper.Mapper<User> {

	@Select("select * from user where name like '%${value}%'")
	public List<User> queryUserByName(String name);

	// 使用UserMapper.xml配置文件
	public List<User> queryAll();
}

6.編寫Service接口

public interface UserService {

	List<User> queryUserByName(String name);
	List<User> queryAll();
	List<User> queryUserByPage(Integer page, Integer rows);
}

編寫實現類:


@Service("userServiceImpl")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public List<User> queryUserByName(String name) {
		List<User> list = this.userMapper.queryUserByName(name);
		return list;
	}

	// 調用使用UserMapper.xml的Mapper
	@Override
	public List<User> queryAll() {
		List<User> list = this.userMapper.queryAll();
		return list;
	}

	// 使用通用Mapper和分頁助手
	@Override
	public List<User> queryUserByPage(Integer page, Integer rows) {
		// 設置分頁
		PageHelper.startPage(page, rows);
		// 使用通用Mapper的方法進行查詢所有數據
		List<User> list = this.userMapper.select(null);
		return list;
	}
}

7.編寫Controller


@RestController
@RequestMapping("user")
public class UserControlelr {

	@Autowired
	private UserService userService;

	@RequestMapping("list/{name}")
	public List<User> queryUserByName(@PathVariable String name) {
		List<User> list = this.userService.queryUserByName(name);
		return list;
	}

	@RequestMapping("list")
	public List<User> queryAll() {
		List<User> list = this.userService.queryAll();
		return list;
	}

	@RequestMapping("list/{page}/{rows}")
	public List<User> queryUserAll(@PathVariable Integer page, @PathVariable Integer rows) {
		List<User> list = this.userService.queryUserByPage(page, rows);
		return list;
	}

}

8.添加靜態頁面

user中id設置爲主鍵自增:@GeneratedValue(strategy-GenerationType.IDENTITY)

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章