简单的SpringBoot+Mybatis框架整合

1.项目目录结构


2.pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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.cn21.test</groupId>
    <artifactId>springboot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <dependencies>
        <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.3.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>
3.数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.typeAliasesPackage=mybatis.po
mybatis.mapperLocations=classpath:/*.xml
注:个别mysql版本要求必须显示声明useSSL否则会报错。
4.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="mybatis.ProductMapper" >
    <insert id="insert" parameterType="po.Product" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO product(num,name)
        VALUES (#{num,jdbcType=INTEGER},#{name,jdbcType=VARCHAR})
    </insert>
</mapper>
5.对应的接口Mapper

package mybatis;

import org.apache.ibatis.annotations.Mapper;
import po.Product;

/**
 * Created by jackcai on 2017/6/21.
 */
@Mapper
public interface ProductMapper {
    int insert(Product po);
    int update(Product po);
    Product select(int id);
}
6.springboot启动程序

@MapperScan("mybatis")
@Controller
@EnableAutoConfiguration
public class SpringBootApp {
    @Autowired
    private ProductMapper mapper;
    @RequestMapping("/hello")
    @ResponseBody
    public Product hello(){
        Product product=new Product();
        product.setName("手机3");
        product.setNum(100);
        mapper.insert(product);
        return product;
    }
    public static void main(String args[]){
        SpringApplication.run(SpringBootApp.class,args);
    }
}
一个简单的springBoot+mybatis项目搭建成功了,启动程序,浏览器输入http://localhost:8080/hello即可看到结果。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值