您的位置:首页 > 编程语言 > Java开发

springboot和mybatis整合(二)

2017-07-31 13:27 295 查看
        上次是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,要用其他的数据连接池需要单独集成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis springboot