Mybatis注解方式
采用springboot+Mybatis的基础整合使用
Mybatis框架提供给我们很方便的对数据库进行相应的操作使用,刚开始用的时候,都是采用配置的方式,Dao的接口,然后在接口的实现类进行crud,而注解的方式有两种,在这里提供一种我经常用到的。
@Mapper注解
还是一样的配置数据源:
server.servlet.context-path=/springdemo
server.port=8888
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.typeAliasesPackage=com.yqx.springbootmybatis.entity
mybatis.mapperLocations=classpath:mapper/*.xml
在Dao接口上定义@Mapper注解,@Param指定参数,与xml中的对应
package com.yqx.springbootmybatis.dao;
import com.yqx.springbootmybatis.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author YangChingyu-k
* @date 2019/8/28 14:31
*/
@Mapper
public interface UserDao {
/**
* 获取用户列表
* @return
*/
List<UserEntity> getUserList();
/**
* 新增
*
* @param userEntity
* @return
*/
boolean addUser(UserEntity userEntity);
/**
* 修改
*
* @param userEntity
* @return
*/
boolean updateUser(UserEntity userEntity);
/**
* 删除
*
* @param id
* @return
*/
boolean deleteById(@Param("id") int id);
/**
* 登录
*
* @param username
* @param password
* @return
*/
UserEntity login(@Param("username") String username, @Param("password") String password);
/**
* 注册
*
* @param username
* @param password
* @return
*/
boolean register(@Param("username") String username, @Param("password") String password);
}
然后映射的xml中还是写好对应的sql块:
<?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.yqx.springbootmybatis.dao.UserDao">
<select id="getUserList" resultType="com.yqx.springbootmybatis.entity.UserEntity">
SELECT * FROM WFW_USER
</select>
<insert id="addUser" parameterType="com.yqx.springbootmybatis.entity.UserEntity">
INSERT INTO WFW_USER (username, password, age, phone, devConf, createTime)
VALUES (#{username}, #{password}, #{age}, #{phone}, #{devConf}, #{createTime})
</insert>
<update id="updateUser" parameterType="com.yqx.springbootmybatis.entity.UserEntity">
UPDATE WFW_USER
<set>
<if test="username != '' and username != null">
USERNAME = #{username},
</if>
<if test="password != '' and password != null">
PASSWORD = #{password},
</if>
<if test="age != '' and age != null">
AGE = #{age},
</if>
<if test="phone != '' and phone != null">
PHONE = #{phone},
</if>
<if test="devConf != '' and devConf != null">
DEVCONF = #{devConf},
</if>
<if test="createTime != '' and createTime != null">
CREATETIME = #{createTime}
</if>
</set>
WHERE ID = #{id}
</update>
<delete id="deleteById">
DELETE FROM WFW_USER WHERE ID = #{id}
</delete>
<select id="login" resultType="com.yqx.springbootmybatis.entity.UserEntity">
SELECT * FROM WFW_USER WHERE USERNAME = #{username} AND PASSWORD = #{password}
</select>
<insert id="register">
INSERT INTO WFW_USER (username, password, age, phone, devConf, createTime)
VALUES (#{username}, #{password}, 0, 0, 0, 0)
</insert>
</mapper>
细节注意的地方就是namespace的指向是我们对应Dao的接口。
这是其中一种我经常用的,还有一种是直接在接口中的方法利用提供给我们的crud注解进行sql的书写,那种方式也可以,但是我个人不喜欢,因为方法上加了很多东西,我觉得不太好看,所以提供这一种。
springboot启动方式注意:
@MapperScan指向的是我们的Dao
package com.yqx.springbootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.yqx.springbootmybatis.dao")
@SpringBootApplication
public class SpringbootmybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootmybatisApplication.class, args);
}
}
Controller利用@Resource进行注入dao
package com.yqx.springbootmybatis.controller;
import com.yqx.springbootmybatis.dao.UserDao;
import com.yqx.springbootmybatis.entity.UserEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
/**
* 用户管理器
*
* @author YangChingyu-k
* @date 2019/8/28 14:40
*/
@Controller
public class UserController {
@Resource
private UserDao userDao;
@RequestMapping("/getUserList")
@ResponseBody
public List<UserEntity> getUserList() {
return userDao.getUserList();
}
@RequestMapping("/register")
@ResponseBody
public boolean register(String username, String password) {
return userDao.register(username, password);
}
}
在一开始使用Mybatis时,我也是会偶尔回顾一下在dao的实现类中去进行原始的方式操作crud,不过方式可能很老了,原始的可以用来回顾,以便于理解,现在框架提供了我们更多简洁方便的使用方法,所以只有把基础原理了解才能更容易对后面的简洁有明白的思路。
谢谢欣赏,如果有其他的讨论欢迎评论,希望可以一起共同进步。~