代码下载地址(含source源码)
项目目录结构
1 pom.xml文件添加mybatis依赖
<!-- mybatis整合springboot jar包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2 application.properties 添加mybatis的配置文件和数据连接配置
#springboot整合Mybatis
#加载Mybatis的配置文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.config-location=classpath:mapper/config/sqlMapConfig.xml
#定义包的别名
mybatis.type-aliases-package=com.example.demo.dto
#配置数据源
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=c##_test_resource
spring.datasource.password=123456
3 java 代码
3.1controller
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("ssm")
public List<UserDto> getAllUsers(){
return userService.getAllUsers();
}
@RequestMapping("id")
public UserDto getUserById(@RequestParam String id){
return userService.getUserById(id);
}
}
3.2 service
@Service
public class UserServiceImpl implements UserService {
//注入Mapper接口的代理对象
@Autowired
private UserMapper userMapper;
@Override
public List<UserDto> getAllUsers() {
return userMapper.getAllUsers();
}
@Override
public UserDto getUserById(String id) {
return userMapper.getUserById(id);
}
}
3.3 mapper
没有实现类
@Mapper
public interface UserMapper {public List<UserDto> getAllUsers();
public UserDto getUserById(String id);
}
4 sql文件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">
<!-- 注意这里namespace一定要是接口的全类,能够点到Mapper接口类中 -->
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="getAllUsers" resultType="UserDto">
select * from boot_user
</select>
<select id="getUserById" parameterType="string" resultType="UserDto">
select * from boot_user u where u.id=#{id}
</select>
</mapper>
5 项目启动入口
/**
* 要管理的代码必须位于这个类的同级,或下级
* @author HeDong
*
*/
@SpringBootApplication
public class SpringBoot01Application {
public static void main(String[] args) {
//项目入口
SpringApplication.run(SpringBoot01Application.class, args);
}
}
6 打包
直接选中项目邮件Run As-->Maven install
即可,jar包会在target中生成
7 运行
7.1 直接双击jar运行,这种方式看不见日志,可在任务管理器中看见javaw.exe进程,就是这个项目进程
7.2 在cmd窗口中执行命令:java -jar spring-boot-01-0.0.1-SNAPSHOT.jar;这种方式可以在cmd中看见日志