springboot+mybatis使用

环境搭建

下载eclipse,创建maven项目

引包

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.1.RELEASE</version>
  </parent>
  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies>
    <!-- Spring Boot Web 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency> 
    <!-- Spring Boot Test 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- Spring Boot JDBC 依赖 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
    <!-- Spring Boot mybatis 依赖 -->
    <dependency>
	    <groupId>org.mybatis.spring.boot</groupId>
	    <artifactId>mybatis-spring-boot-starter</artifactId>
	    <version>1.3.0</version>
	</dependency>
	<!-- Spring Boot mysql 依赖 -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<scope>runtime</scope>
	</dependency>
	<!-- servlet 依赖 -->
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <scope>provided</scope>
	</dependency>
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>jstl</artifactId>
	</dependency>
	 <!-- tomcat 的支持.-->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-tomcat</artifactId>
	    <scope>provided</scope>
	 </dependency>
	<dependency>
	    <groupId>org.apache.tomcat.embed</groupId>
	    <artifactId>tomcat-embed-jasper</artifactId>
	</dependency>
    <!-- Junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  <build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
  </build>

mybatis参数配置

server.port=8083
server.context-path=/server

#mybatis
mybatis.mapper-locations=classpath*:mybatis

#dataSources
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://rm-bp16naif7sq7bj3c1o.mysql.rds.aliyuncs.com:3306/jymamkf?useSSL=false&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=username
spring.datasource.password=password

mybatis的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.li.springboot.test.mapper.ITestMapper">
    <resultMap id="user" type="com.li.springboot.test.vo.User">
        <id property="name" column="name"/>
        <id property="age" column="age"/>
    </resultMap>
    
    <select id="queryUserInfo" parameterType="Object" resultMap="user">
        select NAME,SCOPE age from d_pulic
    </select>
</mapper>

com.li.springboot.test.mapper.ITestMapper 为映射接口路径
com.li.springboot.test.vo.User 实体类路径

实体类

package com.li.springboot.test.vo;
import java.io.Serializable;

public class User implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}
	
}

mybatis映射接口

package com.li.springboot.test.mapper;
import org.mybatis.spring.annotation.MapperScan;
import com.li.springboot.test.vo.User;

@MapperScan
public interface ITestMapper {

	public User queryUserInfo();
}

将ITestMapper接口@Autowired注入其它类就可以使用

项目结构

在这里插入图片描述

启动类

@SpringBootApplication
@MapperScan("com.li.springboot.test.mapper")  //mybatis映射扫描
public class SpringApplicatonRun {

	public static void main(String[] args) {
		SpringApplication.run(SpringApplicatonRun.class, args);
	}
}

在这里插入图片描述

发布了4 篇原创文章 · 获赞 0 · 访问量 96
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览