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

springboot mybatis整合

2017-10-16 14:45 615 查看
1.建立springboot maven项目

2.建立数据库需要用的表



3.建立pom文件中相应的依赖

<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.springboot</groupId>

    <artifactId>springboot05</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.7.RELEASE</version>

    </parent>

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>javax.servlet-api</artifactId>

        </dependency>

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>jstl</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-tomcat</artifactId>

        </dependency>

        <dependency>

            <groupId>org.apache.tomcat.embed</groupId>

            <artifactId>tomcat-embed-jasper</artifactId>

        </dependency>

        <!--连接mysql数据库所需的依赖 -->

        <dependency>

            <groupId>mysql</groupId>

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

            <version>5.1.6</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-jdbc</artifactId>

        </dependency>

        <!--集成mybatis -->

        <dependency>

            <groupId>org.mybatis.spring.boot</groupId>

            <artifactId>mybatis-spring-boot-starter</artifactId>

            <version>1.3.1</version>

        </dependency>

        <!--fastjson -->

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>fastjson</artifactId>

            <version>1.2.38</version>

        </dependency>

    </dependencies>

</project>

4.建立application.properties文件

# 对应的jsp文件路径前缀  src/main/webapp

spring.mvc.view.prefix=/WEB-INF/jsp/

# 对应的jsp文件后缀

spring.mvc.view.suffix=.jsp

# 数据库的配置

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.max-idle=10

spring.datasource.max-wait=10000

spring.datasource.min-idle=5

spring.datasource.initial-size=5

server.port=8080

server.session.timeout=10

server.tomcat.uri-encoding=UTF-8

# 配置需要读取的实体类,这里统一做包的配置

# mybatis.config= classpath:mybatis-config.xml

mybatis.typeAliasesPackage=com.tim.springboot.entity

mybatis.mapperLocations=classpath:mappers/*.xml

5.写相应的类

  1).建立实体类User.java

  


  2).建立Mapper.java 接口

package com.tim.springboot.mapper;

import org.apache.ibatis.annotations.Mapper;

import com.tim.springboot.entity.User;

@Mapper

public interface UserMapper {

    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

}

注意:一定要在mapper上面加上@Mapper注解,如果不加启动会出现

Description:

Field userMapper in com.tim.springboot.controller.MybatisController required a bean of type 'com.tim.springboot.mapper.UserMapper' that could not be found.

Action:

Consider defining a bean of type 'com.tim.springboot.mapper.UserMapper' in your configuration.

这样的错误

如果不加@Mapper的话,启动类必须这样写

@ComponentScan(basePackages="com.tim.springboot.controller")

@MapperScan(basePackages="com.tim.springboot.mapper")

@SpringBootApplication

才不会报以上的错误

  3).建立UserMapper.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.tim.springboot.mapper.UserMapper">

  <resultMap id="BaseResultMap" type="com.tim.springboot.entity.User">

    <id column="id" jdbcType="INTEGER" property="id" />

    <result column="name" jdbcType="VARCHAR" property="name" />

    <result column="age" jdbcType="INTEGER" property="age" />

  </resultMap>

  <sql id="Base_Column_List">

    id, name, age

  </sql>

  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">

    select

    <include refid="Base_Column_List" />

    from user

    where id = #{id,jdbcType=INTEGER}

  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">

    delete from user

    where id = #{id,jdbcType=INTEGER}

  </delete>

  <insert id="insert" parameterType="com.tim.springboot.entity.User">

    insert into user (id, name, age

      )

    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}

      )

  </insert>

  <insert id="insertSelective" parameterType="com.tim.springboot.entity.User">

    insert into user

    <trim prefix="(" suffix=")" suffixOverrides=",">

      <if test="id != null">

        id,

      </if>

      <if test="name != null">

        name,

      </if>

      <if test="age != null">

        age,

      </if>

    </trim>

    <trim prefix="values (" suffix=")" suffixOverrides=",">

      <if test="id != null">

        #{id,jdbcType=INTEGER},

      </if>

      <if test="name != null">

        #{name,jdbcType=VARCHAR},

      </if>

      <if test="age != null">

        #{age,jdbcType=INTEGER},

      </if>

    </trim>

  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.tim.springboot.entity.User">

    update user

    <set>

      <if test="name != null">

        name = #{name,jdbcType=VARCHAR},

      </if>

      <if test="age != null">

        age = #{age,jdbcType=INTEGER},

      </if>

    </set>

    where id = #{id,jdbcType=INTEGER}

  </update>

  <update id="updateByPrimaryKey" parameterType="com.tim.springboot.entity.User">

    update user

    set name = #{name,jdbcType=VARCHAR},

      age = #{age,jdbcType=INTEGER}

    where id = #{id,jdbcType=INTEGER}

  </update>

</mapper>

  4).建立MybatisController.java文件 用于测试

package com.tim.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.tim.springboot.entity.User;

import com.tim.springboot.mapper.UserMapper;

@RestController

public class MybatisController {

    @Autowired

    private UserMapper userMapper;

    @RequestMapping("/mybatis")

    public User getUser() {

        User user = userMapper.selectByPrimaryKey(1);

        System.out.println("成功了!");

        return u
9eba
ser;

    }

}

 5).建立启动类:

package com;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class ApplicationTest {

    

    public static void main( String[] args ){

        SpringApplication.run(ApplicationTest.class, args);

        

    }

}

6.启动项目:

右击项目
点击 run As   ==> Maven clear    ====> maven install  

右击项目
点击 run As ===> java application ===》 在浏览器输入:localhost:8080/mybatis

7.结果显示:



项目总的目录结构:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: