1.pom.xml添加依赖包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
使用mybatis,涉及到操作数据库,所以添加相应的依赖包
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
我使用的是oracle数据库,添加ojdbc依赖包
<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc6 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
2.在springboot配置文件中配置相关的属性
#程序端口(可以不配置,springboot默认启动端口是8080)
server.port=9999
#数据源配置
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.username=sys
spring.datasource.password=123
#连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
#mybatis配置
#对应实体类所在的包
mybatis.typeAliasesPackage=com.example.demo.entity.**
#对应mapper.xml文件
mybatis.mapperLocations=classpath:mybatis/mapper/**/*.xml
#读取mybatis配置
mybatis.configLocation=classpath:mybatis/mybatis-config.xml
3.mybatis-config.xml配置文件(以下只是配置了基本属性,后期若涉及其他地方再添加相应的配置)
<?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>
<!-- 全局参数 -->
<settings>
<!-- 使全局的映射器启用或禁用缓存。 -->
<setting name="cacheEnabled" value="true"/>
<!--数据库的字段名到实体类的属性名的自动映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
4.下面进行操作(对用户信息操作为例)
1.创建数据库表 SYS_USER
-- Create table
create table SYS_USER( id NUMBER not null, login_code VARCHAR2(100), login_name VARCHAR2(100))tablespace CATALOG_MANAGE pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited );-- Create/Recreate primary, unique and foreign key constraints alter table SYS_USER add constraint SYS_USER_ID primary key (ID) using index tablespace CATALOG_MANAGE pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited );
2.创建实体类---->SysUser.java
private Long id;
private String loginCode;
private String loginName;
get...set...(略)
3.创建dao---->SysUserMapper.java
就写两个接口为例
@Mapper
public interface SysUserMapper {
public SysUser getSingle(@Param("id") Long id);
public List<SysUser> getUserList(@Param("id") Long id, @Param("loginCode") String loginCode, @Param("loginName") String loginName);
}
4.创建mapper.xml文件---->SysUser.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 -->
<mapper namespace="com.example.demo.dao.base.mapper.SysUserMapper">
<!-- 根据id获取单个用户 -->
<select id="getSingle" resultType="com.example.demo.entity.base.SysUser">
SELECT t.id,
t.login_code,
t.login_name AS loginName
FROM sys_user t
WHERE 1 = 1
AND t.id = #{id}
</select>
<!-- 查询所有的用户 -->
<select id="getUserList" resultType="HashMap">
select t.* from sys_user t
WHERE 1 = 1
<if test="id != null">
and t.id = #{id}
</if>
<if test="loginCode != null">
and t.login_code = #{loginCode}
</if>
<if test="loginName != null">
and t.login_name = #{loginName}
</if>
</select>
</mapper>
5.创建service---->SysUserService.ava
@Service
public class SysUserService {
@Autowired
private SysUserMapper sysUserMapper;
public SysUser getSingle(Long id){
return sysUserMapper.getSingle(id);
}
public List<SysUser> getUserList(Long id, String loginCode, String loginName){
return sysUserMapper.getUserList(id, loginCode, loginName);
}
}
6.创建controller---->SysUserController.java
@Controller
@RequestMapping(value="/sysUser")
public class SysUserController {
@Autowired
private SysUserService sysUserService;
@RequestMapping(value="/getSingle")
@ResponseBody
public SysUser getSingle(Long id){
return sysUserService.getSingle(id);
}
@RequestMapping(value="/userList")
@ResponseBody
public List<SysUser> getUserList(Long id, String loginCode, String loginName){
return sysUserService.getUserList(id, loginCode, loginName);
}
}
5.调用
http://localhost:9999/sysUser/userList
结果:
[{"LOGIN_CODE":"admin","ID":1,"LOGIN_NAME":"admin"},{"LOGIN_CODE":"hh","ID":2,"LOGIN_NAME":"hh"},{"LOGIN_CODE":"tt","ID":3,"LOGIN_NAME":"tt"}]