标签:artifact 技术 tis style bind app pac for from
这里介绍注解xml整合SpringBoot和Mybatis的模式。
开发环境
创建项目
项目结构
Maven依赖
这里的数据库连接池使用阿里的。
<?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.example</groupId> <artifactId>demo2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo2</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <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.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.25</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
SpringBoot配置文件application.yml。配置数据库连接池和mybatis
spring: datasource: username: root password: 1234 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/customers mybatis: mapper-locations: classpath:sqlmap/UserMapper.xml type-aliases-package: org.springboot.sample.entity
映射类,使用mapper注解即可
package com.example.demo.mapper; import org.apache.ibatis.annotations.Mapper; /** * Created by xiaojiang on 2018/5/5. */ @Mapper public interface UserMapper { String selectByPrimaryKey(int cid); }
映射文件
<?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.example.demo.mapper.UserMapper"> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultType="java.lang.String"> select phone from customer where cid=#{cid} </select> </mapper>
Service类
package com.example.demo.service; import com.example.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.Serializable; /** * Created by xiaojiang on 2018/5/5. */ @Service public class UserService { @Autowired private UserMapper userMapper; public String select(int cid){ return userMapper.selectByPrimaryKey(cid); } }
Controller类
package com.example.demo.controller; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by xiaojiang on 2018/5/5. */ @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/user") public String se(int cid){ return userService.select(cid); } }
测试:运行Demo2Application.java。打开网页访问。
附上github 代码:https://github.com/xiaojiang1230/demo2
标签:artifact 技术 tis style bind app pac for from
原文地址:https://www.cnblogs.com/xiaolovewei/p/8995199.html