本博客猫叔的博客,转载请申明出处
本系列教程为 HMStrange项目 附带。
备注:本系列开发工具均为IDEA
大家可以看看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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.myself.mybatis</groupId> <artifactId>datademo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>datademo</name> <description>Demo project for Spring Boot</description> <properties> <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>2.0.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </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>
DDL
CREATE TABLE `t_msg` ( `id` int(11) NOT NULL, `message` varchar(255) DEFAULT NULL COMMENT '信息', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
spring: datasource: url: jdbc:mysql://192.168.192.133:3306/datademo?characterEncoding=utf-8&useSSL=false username: root password: password driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath*:mapper/*Mapper.xml type-aliases-package: com.myself.mybatis.entity
package com.myself.mybatis.entity; import lombok.Data; import java.io.Serializable; /** * Created by MySelf on 2019/4/9. */ @Data public class TMsg implements Serializable { private Integer id; private String message; }
package com.myself.mybatis.mapper; import com.myself.mybatis.entity.TMsg; import org.apache.ibatis.annotations.Mapper; /** * Created by MySelf on 2019/4/9. */ @Mapper public interface TMsgMapper { public TMsg findById(Integer id); }
<?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.myself.mybatis.mapper.TMsgMapper"> <select id="findById" resultType="com.myself.mybatis.entity.TMsg"> SELECT id,message from t_msg WHERE id = #{id} </select> </mapper>
我这边就单纯一个方法,大家可以扩展自己的方法。
package com.myself.mybatis.service; import com.myself.mybatis.entity.TMsg; /** * Created by MySelf on 2019/4/9. */ public interface TMsgService { public TMsg findById(Integer id); }
package com.myself.mybatis.service.impl; import com.myself.mybatis.entity.TMsg; import com.myself.mybatis.mapper.TMsgMapper; import com.myself.mybatis.service.TMsgService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by MySelf on 2019/4/9. */ @Service public class TMsgServiceImpl implements TMsgService { @Autowired private TMsgMapper tMsgMapper; @Override public TMsg findById(Integer id) { return tMsgMapper.findById(id); } }
package com.myself.mybatis.controller; import com.myself.mybatis.entity.TMsg; import com.myself.mybatis.service.TMsgService; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by MySelf on 2019/4/9. */ @RestController @RequestMapping("/msg") public class TMsgController { @Autowired private TMsgService tMsgService; @GetMapping("/getMsg") public String getMsg(@Param("id") Integer id){ TMsg tMsg = tMsgService.findById(id); return tMsg.getMessage(); } }
欢迎到HMStrange项目进行下载:https://github.com/UncleCatMySelf/HMStrange/tree/master/doc/demo/datademo
现架构设计(码农)兼创业技术顾问,不羁平庸,热爱开源,杂谈程序人生与不定期干货。
Previous
Centos7.6安装MySQL+Redis(最新版)