1.基础类使用mapper
@RequestMapping("")
@MapperScan("awsa.jatham.cloud.platform.mapper")
@Component
public class H2Client {
//注入mappper
@Autowired
private UserMapper userMapper;
@Autowired
private DataNodeMapper dataNodeMapper;
public static H2Client h2Client;
@PostConstruct //导入bean
public void init(){
h2Client = this;
h2Client.dataNodeMapper = this.dataNodeMapper;
}
2.mybatis&mysql 字段为关键字问题
//使用反引号解决 esc下面的键位
<insert id="insert" parameterType="awsa.jatham.cloud.platform.entity.Constellation" keyProperty="id">
insert into constellation(date,name,`number`,`all`)
values(#{date,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{number,jdbcType=INTEGER},
#{all,jdbcType=VARCHAR})
</insert>
3.springboot整合mybatis流程
-
添加依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency>
-
编写简单java类
-
编写mapper接口 设计需要实现的方法
@Repository(value = "UserMapper")
public interface UserMapper {
User getById(int id);
public boolean insert(String name);
public List<User> getUsers();
public boolean updateUser(User user);
public boolean deleteUser(int id);
public boolean deleteAllUsers();
}
- 编写mapper.xml实现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="awsa.jatham.cloud.platform.mapper.UserMapper">
<!--id对应接口中的方法,名字要一样,parameterType是方法的参数类型,
resultType是查询返回的类型,需要注意的是,这里的SQL语句后面不能加分号,变量不能加引号-->
<select id="getById" parameterType="int" resultType="awsa.jatham.cloud.platform.entity.User">
select * from user where id = #{id}
</select>
<insert id="insert" parameterType="string">
insert into user(name) values(#{name})
</insert>
<select id="getUsers" resultType="awsa.jatham.cloud.platform.entity.User">
select * from user order by age
</select>
<update id="updateUser" parameterType="awsa.jatham.cloud.platform.entity.User">
update user set name=#{name} where id = #{id}
</update>
<delete id="deleteAllUsers">
delete from user
</delete>
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
</mapper>
5.编写 Service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getById(int id){
return userMapper.getById(id);
}
public List<User> getByUsers()
{
return userMapper.getUsers();
}
}
6.controller里编写接口调用
@CrossOrigin // 解决跨域问题
@RestController
@RequestMapping("")
public class IndexController {
//H2Client h2Client = new H2Client();
private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
H2Client h2Client = new H2Client();
HashMap<String,Integer>params = new HashMap<String, Integer>();
@Autowired //自动装配
private UserService userService;
@Autowired
private DataNodeMCUService dataNodeMCUService;
@GetMapping("/dataNodeMCU")
public StringBuilder index()
{
logger.info(""+h2Client.dataFromAli());
System.out.println(h2Client.dataFromAli());
return h2Client.dataFromAli();
}
@PostMapping("/dataInsert")
public void insertData(@RequestParam(value = "ifHuman",required = true)Integer ifHuman)
{
params.clear();
params.put("ifHuman",ifHuman);
dataNodeMCUService.insertBatch(params);
}
@GetMapping("/users")
public List<User> user()
{
return userService.getByUsers();
}
最后配置.yml或者properties
#spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
# 定义匹配静态资源路径
spring.mvc.static-path-pattern=/**
# 定义静态资源位置
spring.resources.static-locations=classpath:/templates,classpath:/resources/,classpath:/static/,classpath:/public/
#datasource
spring.datasource.tomcat.driver-class-name=com.mysql.jdbc.Driver
#mybatis
spring.datasource.url=jdbc:mysql://101.xxx.xxx.199:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true&useSSL=false
spring.datasource.username=
spring.datasource.password=
spring.datasource.tomcat.default-auto-commit=true
mybatis.typeAliasesPackage= awsa.jatham.cloud.platform.mapper
mybatis.mapper-locations=classpath:mapper/*.xml