JAVA MyBatis使用

1、XMl MyBatis

<?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.jsapi002.spring</groupId>
<artifactId>boot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!--Spring Boot项目需要指定的父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<!--Spring Boot Web项目的starter包,Web项目引入此包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Boot项目数据库读写的starter包,需要数据库读写是引入此包即可-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--Spring Boot默认情况下不支持JSP模板,推荐使用thymeleaf模板引擎,引入此starter即可-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--WebSocket支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!--mysql连接器-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<!--Spring Boot提供的项目打为Jar包的Maven插件,执行mvn package即可打为可启动jar包。-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

2、propertis
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=19930228

mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.type-aliases-package=com.jsapi002.spring.mapper
3、resource/mapper/common.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.jsapi002.spring.mapper.Commondbmapper">
<select id="getList" resultType="com.jsapi002.spring.User">
select * from rd_sample where name_cn = "SV681"
</select>

</mapper>

4、Commondbmapper
package com.jsapi002.spring.mapper;

import com.jsapi002.spring.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

@Mapper
@Component
public interface Commondbmapper {
public User getList();
}

5、使用

package com.jsapi002.spring.controller;

import com.jsapi002.spring.User;
import com.jsapi002.spring.mapper.Commondbmapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Component
public class TikDemo{
@Autowired
private Commondbmapper commondbmapper;

@RequestMapping("/rest")
public String com() {
User res = commondbmapper.getList();
return res.toString();
}
}

6、启动

package com.jsapi002.spring;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.jsapi002.spring.mapper")
public class Main {

public static void main(String[] args) {
SpringApplication.run(Main.class);
}
}
posted @ 2021-04-09 22:20  jsapi007  阅读(83)  评论(0)    收藏  举报