springboot和mybatis整合(二)

        上次是springboot的简单入门。显然,这个对于我们意义并不是很大。我们需要的是一个完整的框架。然后就选择和mybatis进行整合。对于springboot和mybatis整合其实还是比较简单的。大致springboot提供了各种starter,比如mybatis提供mybatis-spring-boot-starter。我们只要添加依赖就好了。

1.添加相关依赖。

<!--mybatis-->

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

<!--mysql-->

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

2.配置文件

springboot还是有一个配置文件的。叫application.yml或application.properties。特别强调的是yml文件的语法,想要表达键值对就是: (冒号后面跟空格。)

spring.datasource.url: jdbc:mysql://localhost:3306/jiaxu?useUnicode=true&characterEncoding=utf8
spring.datasource.username: root
spring.datasource.password: jiaxu123
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
mybatis.typeAliasesPackage: com.cjx.firstSpringboot.pojo
mybatis.mapperLocations: classpath:mapper/*.xml

#  server.port=8888  访问端口
#  server.address=    访问路径

这是mybatis相关的配置。指定mapper.xml地址和实体类


3.配置差不多了。跑个demo看一看

启动类

package com.cjx.firstSpringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;

@MapperScan("com.cjx.firstSpringboot.mapper")
@EnableAutoConfiguration
@ComponentScan("com.cjx.firstSpringboot.*")
@EnableTransactionManagement
public class SampleController {

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


controller


package com.cjx.firstSpringboot.controller;


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cjx.firstSpringboot.pojo.User;
import com.cjx.firstSpringboot.service.UserService;

@RequestMapping("/")
@Controller
@EnableAutoConfiguration
public class UserController {
    @Autowired    
    private UserService userService;
    
    @RequestMapping("/list/all")
    @ResponseBody
    public List<User> listAll() {
        return userService.selectAll("cjx");
    }
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
       return "hello";
    }

}

service

package com.cjx.firstSpringboot.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cjx.firstSpringboot.mapper.UserMapper;
import com.cjx.firstSpringboot.pojo.User;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public List<User> selectAll(String name) {
        return userMapper.selectAll(name);
    }

}


mapper

package com.cjx.firstSpringboot.mapper;

import java.util.List;

import com.cjx.firstSpringboot.pojo.User;

public interface UserMapper {
    List<User> selectAll(String name);

}


pojo

package com.cjx.firstSpringboot.pojo;

import java.io.Serializable;

@SuppressWarnings("serial")
public class User implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

}

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.cjx.firstSpringboot.mapper.UserMapper">
 
  <select id="selectAll" resultType="com.cjx.firstSpringboot.pojo.User">
    select * from user;
  </select>
</mapper>

当然,sql可以不再xml中写。直接在对于的方法上加注解写sql,比如

   @Select("select * from wx_userinfo;")
    public Map<String,Object> find();

结果出了个异常:Cannot determine embedded database driver class for database type NONE

配置文件符号搞错了


另注:这只是集成mybatis,要用其他的数据连接池需要单独集成




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值