spring boot整合mybatis

                                                                                        spring boot整合mybatis

        ORM框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是hibernate,一个是可以灵活调试动态sql的mybatis,两者各有特点,在企业级系统开发中可以根据需求灵活使用。发现一个有趣的现象:传统企业大都喜欢使用hibernate,互联网行业通常使用mybatis。

        hibernate特点就是所有的sql都用Java代码来生成,不用跳出程序去写(看)sql,有着编程的完整性,发展到最顶端就是spring data jpa这种模式了,基本上根据方法名就可以生成对应的sql了

        Mybatis最大的特点是:sql语句需要程序员来写,对sql书写有一定的要求。但是同时,也发挥了sql灵活性的价值。

mybatis-spring-boot-starter就是springboot+mybatis可以完全注解不用配置文件,也可以简单配置轻松上手。

        其实就是myBatis看spring boot这么火热也开发出一套解决方案来凑凑热闹,但这一凑确实解决了很多问题,使用起来确实顺畅了许多。mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解解决一切问题一种是简化后的xml配置sql,自动扫描注入接口实现基于注解的方式,对于简单的实现非常容易,但涉及复杂的sql语句操作就显得捉襟见肘。第二种xml装配sql的方式,更加常用。

        注意这里创建的是Spring Boot工程,不是Spring MVC

注解版(无配置文件)

1、添加maven依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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>
    </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

2、application.properties数据库配置

mybatis.type-aliases-package=com.test.boot.entity

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://172.18.81.146:3306/test
spring.datasource.username = lee
spring.datasource.password = lee017

        springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,让你你一切都不用管了,直接拿起来使用就行了。

在启动类中添加对mapper包扫

@SpringBootApplication
@MapperScan(value ="com.test.mapper")
public class BootApplication {

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

3Mapper接口

是最关键的一块,sql生产都在这里

public interface UserMapper {

    @Select("SELECT id,name,passwd,appid FROM user WHERE id =#{id}")
    User getOne(Integer id);


    @Select("SELECT * FROM user")
    List<User> getAll();


    @Delete("DELETE FROM user WHERE id =#{id}")
    void delete(Integer id);

    @Insert("INSERT INTO user(name,passwd,appid) VALUES(#{name}, #{passwd}, #{appid})")
    void insert(User user);
}

Mapper接口中用注解,描述sql的操作,根据sql动态完成接口的Impl

· @Select 是查询类的注解,所有的查询均使用这个

· @Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。

· @Insert 插入数据库使用,直接传入实体类会自动解析属性到对应的值

· @Update 负责修改,也可以直接传入对象

· @delete 负责删除

4、编写Controller

package com.test.controller;

import com.test.boot.entity.User;
import com.test.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {

    @Autowired(required = false)
    private UserMapper userMapper;


    //-http://localhost:8080/getUser?id=1
    @RequestMapping(value="/getUser",method = RequestMethod.GET)
    public User getUser(@RequestParam("id") Integer id) {
        User user=userMapper.getOne(id);
        return user;
    }

    @RequestMapping(value="/getUsers",method = RequestMethod.GET)
    public List<User> getUsers() {
        return userMapper.getAll();
    }

    //-http://localhost:8080/add?name=abc&passwd=123&appid=95955542783
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public void save(@RequestParam("name") String name,
                     @RequestParam(value = "passwd",defaultValue = "0") String passwd,
                     @RequestParam(value = "appid",defaultValue = "0") String appid) {
        userMapper.insert(new User(name,passwd,appid));
    }


    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public void delete(@PathVariable("id") Integer id) {
        userMapper.delete(id);
    }
}

测试:

 

 

源码下载:http://download.csdn.net/detail/ljheee/9919393

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值