springboot整合mybatis

2019-03-03  本文已影响0人  一只小安仔

springboot整合mybatis

先看application.yml配置


#默认使用配置
spring:
  profiles:
    active: dev
server:
  port: 8081


#公共配置与profiles无关:
mybatis:
  type-aliases-package: com.chenjunan.pojo
  mapper-locations: classpath:mappers/*.xml

---


spring:

  profiles: dev

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/spring-shiro
    username: root
    password: chenjunan
    type: com.alibaba.druid.pool.DruidDataSource

yml语法这里不做细讲,自行百度,当然也可以讲配置文件的内容下载properties文件里,个人觉得yml看着舒服。yml基础语法
mybatis下的type-aliases-package用于指定相应的pojo,domain,或者entrty存放包。mapper-locations用于指定mapper.xml文件位置,注意这里的classpath表示resources下。一般在resources下建个mappers文件夹存放所有的mapper配置文件。
数据源这里注意mysql的driver官方推荐写com.mysql.cj.jdbc.Driver。
type表示使用的数据源类的定位。(我这里使用的阿里的druid,若使用dbcp,c3p0自行百度哦!)

#mapper示例:

package com.chenjunan.mapper;

import com.chenjunan.pojo.User;

public interface UserMapper {


    /**
     * 通过姓名查找user
     * @param name
     * @return
     */
    User findByName(String name);
    
    
}

#mapper.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.chenjunan.mapper.UserMapper">

  <select id="findByName" parameterType="String" resultType="User">
     select * from user where user_name = #{value}

  </select>

</mapper>

这里只是简单做了通过name向数据库查询user的操作。

如果需要springboot扫描到mapper类需要在springboot启动类上添加注解@mapperscan
例:

package com.chenjunan;

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

@SpringBootApplication
@MapperScan("com.chenjunan.mapper")  //mapper包路径
public class SpringShiroApplication {

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

}

#差点忘记,还有service和serviceImpl

service

package com.chenjunan.service;

import com.chenjunan.pojo.User;

public interface UserService {


    User findByName(String username);

}

serviceImpl

package com.chenjunan.service.impl;

import com.chenjunan.service.UserService;
import com.chenjunan.mapper.UserMapper;
import com.chenjunan.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByName(String username) {
        return userMapper.findByName(username);
    }


}

最后是单元测试

package com.chenjunan;

import com.chenjunan.pojo.User;
import com.chenjunan.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringShiroApplicationTests {

    @Autowired
    private UserService userService;


    @Test
    public void mybatisTest() {

        User user = userService.findByName("chenjunan");
        
        System.out.println(user);
        
    }

}

结果如下: mybatis.png

可以看出,结果完全正确,整合到此结束。
如有错误,请指正,万般感谢!

上一篇下一篇

猜你喜欢

热点阅读