微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringBoot_MyBatis

一、引入依赖

<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.1.4</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
				<dependency>
			<groupId>MysqL</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

二、添加application.properties配置

# mybatis的别名包扫描
mybatis.type-aliases-package=org.lanqiao.bean

spring.datasource.url=jdbc:MysqL://124.70.142.213:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=qwe123!@#
spring.datasource.driver-class-name=com.MysqL.cj.jdbc.Driver
#新增
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

三、添加mybaitis的配置文件、虽然为空、保留下来以后可能会添加其他配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>

四、添加dao层接口、既可使用注解、亦可使用mapper配置文件

public interface StudentDao {
	public List<Student> getAll();
	
	@Select("select * from student")
	public List<Student> getAll2();
}

五、添加dao层的mapper配置文件

<?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="org.lanqiao.dao.StudentDao">
	<select id="getAll" resultType="student">
		SELECT * FROM student
	</select>
</mapper>

六、给启动类添加dao成mapper扫描

@SpringBootApplication(scanBasePackages = "org.lanqiao")
@MapperScan("org.lanqiao.dao")
public class Springboot002Application {
	public static void main(String[] args) {
		SpringApplication.run(Springboot002Application.class, args);
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐