简单的SpringBoot+Mybatis框架整合

来源:互联网 发布:linux crontab 重启 编辑:程序博客网 时间:2024/05/18 03:19

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=UTCspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.cj.jdbc.Drivermybatis.typeAliasesPackage=mybatis.pomybatis.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. */@Mapperpublic interface ProductMapper {    int insert(Product po);    int update(Product po);    Product select(int id);}
6.springboot启动程序

@MapperScan("mybatis")@Controller@EnableAutoConfigurationpublic 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即可看到结果。