一 步骤:
1 新建项目
新建springboot项目,步骤参考https://blog.csdn.net/wqx484/article/details/88684662
最后的目录结构为:
2 类代码
创建好需要的包和类:直接上代码
我这里是查询一个简单的emp员工表
EmpBean类:
public class EmpBean {
private Integer empid;
private String empname;
private Integer age;
private String sex;
private String pwd;
private Integer deptid;
private String face;
//set get tostring方法省略
}
Controller类
package com.ht.controller;
import com.ht.bean.EmpBean;
import com.ht.service.IEmpService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by wqx on 2019/3/22.
*/
@Controller
@RequestMapping("/emp")
public class EmpController {
@Resource
private IEmpService is;
@RequestMapping("/emplist")
public String selemplist(){
List<EmpBean> emplist=is.selemp();
System.out.println(emplist);
return "ok";
}
}
dao类:
package com.ht.dao;
import com.ht.bean.EmpBean;
import java.util.List;
/**
* Created by wqx on 2019/3/22.
*/
public interface EmpDao {
List<EmpBean> selemplist();
}
Service接口类
package com.ht.service;
import com.ht.bean.EmpBean;
import java.util.List;
/**
* Created by wqx on 2019/3/22.
*/
public interface IEmpService {
//查询列表
List<EmpBean> selemp();
}
Service实现类
package com.ht.service.impl;
import com.ht.bean.EmpBean;
import com.ht.dao.EmpDao;
import com.ht.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Created by wqx on 2019/3/22.
*/
@Service
@Transactional
public class EmpServiceImpl implements IEmpService {
@Autowired
private EmpDao dao;//这里会报错,但是不影响项目
@Override
public List<EmpBean> selemp() {
return dao.selemplist();
}
}
EmpDaoMapping.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.ht.dao.EmpDao">
<select id="selemplist" resultType="empbean">
<![CDATA[
select * from emp
]]>
</select>
</mapper>
启动类SpringbootSpringmvcMybatisApplication
package com.ht;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.ht.dao")
public class SpringbootSpringmvcMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSpringmvcMybatisApplication.class, args);
}
}
3 配置文件
1 Pom.xml文件:
<?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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ht</groupId>
<artifactId>springboot_springmvc_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_springmvc_mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--jdbc启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--mybatis启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 阿里巴巴druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</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>
2 在resources目录下的application.properties文件
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=wuqixin
#阿里巴巴连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#扫描mapping映射文件
mybatis.mapperLocations=classpath*:mapper/*.xml
#配置别名
mybatis.type-aliases-package=com.ht.bean
3 在resources/mapper目录下的EmpDaoMapping.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.ht.dao.EmpDao">
<select id="selemplist" resultType="empbean">
<![CDATA[
select * from emp
]]>
</select>
</mapper>
4测试
浏览器输入localhost:8080/emp/emplist
成功
二 需要注意的点(重要)
在搭建过程中容易踩的坑
1、在springboot启动类中需要配置dao包的扫描注解
2、在IDEA中整合需要把mapping.xml映射文件放到resources/mapping目录下,并且需要在application.properties文件中配置扫描mapping.xml映射文件
3、dao接口名称不需要和mapping映射文件名称相同
只需要在mapping映射文件中指定Dao接口的路径即可