使用mybatis官方提供的Spring Boot整合包实现
1.application.properties中配置整合mybatis的配置文件、mybatis扫描别名的基本包与数据源
server.port=80
logging.level.org.springframework=DEBUG
#springboot mybatis
#jiazai mybatis peizhiwenjian
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
mybatis.type-aliases-package = cn.qlq.bean
#shujuyuan
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456
2.pom.xml加入springboot整合mybatis的jar包与数据库驱动包
<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.0modelVersion>
<groupId>cn.qlqgroupId>
<artifactId>springboot-ssmartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.2.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.9version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jsp-apiartifactId>
<version>2.0version>
<scope>providedscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.5.1version>
<configuration>
<source>1.7source>
<target>1.7target>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
3.mybatis全局配置文件:
sqlMapConfig.xml
<configuration>
configuration>
4.编写代码:
(1)User.java
package cn.qlq.bean;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
(2)UserMapper.java (注意接口上的注解是@mapper,代替之前扫描接口的操作)
package cn.qlq.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import cn.qlq.bean.User;
@Mapper
public interface UserMapper {
public List<User> findAll();
}
UserMapper.xml
<mapper namespace="cn.qlq.mapper.UserMapper">
<select id="findAll" resultType="user">
select * from user
select>
mapper>
(3)UserService.java
package cn.qlq.service;
import java.util.List;
import cn.qlq.bean.User;
public interface UserService {
/**
* 根据接口查询所用的用户
*/
public List<User> findAllUser();
}
UserServiceImpl.java
package cn.qlq.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.qlq.bean.User;
import cn.qlq.mapper.UserMapper;
import cn.qlq.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public List<User> findAllUser() {
List<User> list = userMapper.findAll();
return list;
}
}
(4)UserController.java
package cn.qlq.action;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.qlq.bean.User;
import cn.qlq.service.UserService;
@RestController/**自动返回的是json格式数据***/
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("list")
public List<User> list(){
List<User> list = userService.findAllUser();
return list;
}
}
注意:@RestController 注解 (自动返回的是json格式数据)
(5)测试类
package cn.qlq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
//入口运行类
SpringApplication.run(MySpringBootApplication.class, args);
}
}
git源码地址: https://github.com/qiao-zhi/springboot-ssm
转载至:https://www.cnblogs.com/qlqwjy/p/8463822.html