spring-boot 整合mybatis

pom.xml配置如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zyc</groupId>
	<artifactId>SpringBootDemo</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBootDemo Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.4.RELEASE</version>
	</parent>

	<dependencies>
	<!-- 	<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency> -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
		    <groupId>org.mybatis.spring.boot</groupId>
		    <artifactId>mybatis-spring-boot-starter</artifactId>
		    <version>1.1.1</version>
		</dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

	</dependencies>
	<build>
		<finalName>SpringBootDemo</finalName>
		<!-- <plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins> -->
	</build>
</project>

application.properties配置如下

spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.typeAliasesPackage=com.zyc.springboot.entity
mybatis.mapperLocations=classpath\:mapper/*.xml

项目的结构如下:

LoginController.java如下

package com.zyc.springboot.controller;

import java.util.List;

import javax.annotation.Resource;

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 org.springframework.web.bind.annotation.RestController;

import com.zyc.springboot.entity.Role;
import com.zyc.springboot.service.RoleService;

@Controller
public class LoginController {

	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/hello")
	@ResponseBody
	public String hello(){
		
		return "index";
	}
	@RequestMapping("/getRole")
	@ResponseBody
	public String getRole(){
		List<Role> role = roleService.getRole();
		System.out.println(role.get(0).getRoleName()+"=="+role.get(0).getId());
		
		return "index";
	}
}

dao层如下

package com.zyc.springboot.dao;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.zyc.springboot.entity.Role;

//@Mapper
public interface RoleDao {

	//@Select(value="select id ,role_name as roleName from role")
	public List<Role> getRole();
}
service层如下

package com.zyc.springboot.service.impl;

import java.util.List;

import javax.annotation.Resource;

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

import com.zyc.springboot.dao.RoleDao;
import com.zyc.springboot.entity.Role;
import com.zyc.springboot.service.RoleService;

@Service("RoleService")
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;
	
	@Override
	public List<Role> getRole() {
		// TODO Auto-generated method stub
		return roleDao.getRole();
	}

}

role.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.zyc.springboot.dao.RoleDao">
<resultMap type="com.zyc.springboot.entity.Role" id="roleMap">
<result column="role_name" property="roleName"/>
<result column="id" property="id"/>
</resultMap>
	<select id="getRole" resultMap="roleMap">
	select id,role_name from role
	</select>
</mapper>

启动类如下

package com.zyc.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.zyc.springboot.dao")
@SpringBootApplication
public class ApplicationRun {

	public static void main(String[] args) {
		
    SpringApplication.run(ApplicationRun.class, args);
	}

}

上述使用的是

@MapperScan("com.zyc.springboot.dao")方式如果在dao层 想使用@Mapper注解 则只需把@MapperScan注解去掉,application.properties中
mybatis.typeAliasesPackage=com.zyc.springboot.entity
mybatis.mapperLocations=classpath\:mapper/*.xml
也要去掉
2种方式任选一种即可



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值