springboot-整合mybatis

1.idea创建springboot工程

工程目录

2.  pom.xml依赖如下

这里最主要的就是mybatis-spring-boot-starter的依赖,

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example.lujia</groupId>
	<artifactId>springboot-mybttis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>springboot-mybttis</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<!-- 使用druid数据源-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
			<scope>true</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<fork>true</fork>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

3. application.properties配置文件

#数据库连接属性
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#使用alibaba druid datasource
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.stat-view-servlet.allow=true
#mybatis实体的包路径
mybatis.type-aliases-package=com.example.lujia.domain
#mybatis  mapper.xml的路径
mybatis.mapper-locations=classpath:mybatis/*.xml

这里还可以添加mybaties的一些配置

mybatis.config = mybatis 配置文件名称
mybatis.mapperLocations = mapper xml 文件地址
mybatis.typeAliasesPackage = 实体类包路径
mybatis.typeHandlersPackage = type handlers 处理器包路径
mybatis.check-config-location = 检查 mybatis 配置是否存在,一般命名为 mybatis-config.xml
mybatis.executorType = 执行模式。默认是 SIMPLE
4. domain类

package com.example.lujia.domain;

/**
 * Created by LuTshoes on 2017/7/18 0018.
 * lutshoes@163.com
 */
public class Language {
    private Long id;
    private String name;
    private Integer age;

    public Language() {
    }

    public Language(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

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

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

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

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }
}

4. mapper接口,可以在接口上生面@mapper注解

package com.example.lujia.mapper;

import com.example.lujia.domain.Language;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * Created by LuTshoes on 2017/7/18 0018.
 * lutshoes@163.com
 */
//此处可以使用@mapper注解代替启动类中的@MapperScan("com.example.lujia.mapper")
@Mapper
public interface LanguageMapper {

    Language findById(@Param("id") Long id);
}

5.service   和serviceimpl

package com.example.lujia.service;

import com.example.lujia.domain.Language;

/**
 * Created by LuTshoes on 2017/7/18 0018.
 * lutshoes@163.com
 */
public interface LanguageService {
    Language findById(Long id);
}


serviceimpl

package com.example.lujia.service.impl;

import com.example.lujia.domain.Language;
import com.example.lujia.mapper.LanguageMapper;
import com.example.lujia.service.LanguageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by LuTshoes on 2017/7/18 0018.
 * lutshoes@163.com
 */
@Service
public class LanguageServiceImpl implements LanguageService{
    @Autowired
    private LanguageMapper languageMapper;
    @Override
    public Language findById(Long id) {
        return languageMapper.findById(id);
    }
}


6.controller

package com.example.lujia.web;

import com.example.lujia.domain.Language;
import com.example.lujia.service.LanguageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * Created by LuTshoes on 2017/7/18 0018.
 * lutshoes@163.com
 */
@Controller
public class LanguageController {
    private static Logger logger= LoggerFactory.getLogger(LanguageController.class);
    @Autowired
    private LanguageService languageService;
    @RequestMapping("findById")
    public @ResponseBody Language findById(Long id){
        logger.info("find by id");

        return languageService.findById(id);
    }
}

7.mybatis.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">
<!-- namespace必须指向Dao接口 -->
<mapper namespace="com.example.lujia.mapper.LanguageMapper">

    <!-- 所有列 -->
    <sql id="Column_list">
        id,
        name,
        age
    </sql>

    <resultMap id="LanguageMap" type="com.example.lujia.domain.Language" >
        <id  column="id" property="id" />
        <result column="name" property="name" />
        <result column="age" property="age" />
    </resultMap>

    <!-- 根据ID查询数据 -->
    <select id="findById" parameterType="long" resultMap="LanguageMap">
        SELECT
        <include refid="Column_list" />
        FROM language
        WHERE id = #{id}
    </select>
</mapper>
8.springboot启动类

package com.example.lujia;

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

@SpringBootApplication

//如果mapper类中配置了@mapper注解,此处可以不用写@MapperScan
 /*@MapperScan("com.example.lujia.mapper")*/
public class SpringbootMybttisApplication {

private static Logger logger= LoggerFactory.getLogger(SpringbootMybttisApplication.class);
	public static void main(String[] args) {
		SpringApplication.run(SpringbootMybttisApplication.class, args);
        logger.info("spring boot start success");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值