SpringBoot整合TkMybatis(通用mapper)(自定义xml文件)

6 篇文章 2 订阅
2 篇文章 0 订阅

TkMybatis目录

  1. SpringBoot整合TkMybatis(通用mapper)
  2. SpringBoot整合TkMybatis(通用mapper)(自定义xml文件)

源码

GitHub: https://github.com/291685399/springboot-learning/tree/master/springboot-tkmybatis02

前一篇文章说了怎么使用tkmybatis中给我们提供的CRUD接口,直接调用即可,但是很多情况下提供的接口并不满足所有需求,那么就需要我们自己配置xml文件了

Springboot整合tkmybatis

pom.xml:

<dependencies>
    <!-- springboot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
    <!-- tkmybatis -->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper</artifactId>
        <version>4.1.5</version>
    </dependency>
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.1.5</version>
    </dependency>
    
    <!-- mysql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

UserController:

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findById/{id}")
    public User findById(@PathVariable int id) {
        User user = userService.findById(id);
        return user;
    }

    @RequestMapping("/findAll")
    public List<User> findAll() {
        List<User> userList = userService.findAll();
        return userList;
    }

    @RequestMapping("/insert")
    public void insert() {
        User user = new User();
        user.setName("张三");
        user.setSex("男");
        user.setAge(18);
        user.setAddress("江西省");
        user.setPhone("456789645");
        userService.insert(user);
    }

    @RequestMapping("/delete")
    public void delete() {
        User user = new User();
        user.setId(5);
        userService.delete(user);
    }

    @RequestMapping("/update")
    public void update() {
        User user = new User();
        user.setId(5);
        user.setName("李四");
        userService.update(user);
    }
}

UserService:

public interface UserService {

    public User findById(int id);

    public List<User> findAll();

    public void insert(User user);

    public void update(User user);

    public void delete(User user);

}

UserServiceImpl:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findById(int id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectUserList();
    }

    @Override
    public void insert(User user) {
        userMapper.insertUser(user);
    }

    @Override
    public void update(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void delete(User user) {
        userMapper.deleteUser(user);
    }
}

UserMapper:

public interface UserMapper extends Mapper<User> {

    public void insertUser(User user);

    public void updateUser(User user);

    public void deleteUser(User user);

    public User selectUserById(int id);

    public List<User> selectUserList();
}

UserMapper.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="com.wyj.mapper.UserMapper">

    <insert id="insertUser" parameterType="com.wyj.entity.po.User">
        insert into `user`(name,sex,age,address,phone) values (#{name},#{sex},#{age},#{address},#{phone})
    </insert>

    <update id="updateUser" parameterType="com.wyj.entity.po.User">
        update `user`
        <set>
            <if test="name != null and name != ''">
                name=#{name},
            </if>
            <if test="sex != null and sex != ''">
                sex=#{sex},
            </if>
            <if test="age != 0">
                age=#{age},
            </if>
            <if test="address != null and address != ''">
                address=#{address},
            </if>
            <if test="phone != null and phone != ''">
                phone=#{phone},
            </if>
        </set>
        where id=#{id}
    </update>

    <delete id="deleteUser" parameterType="com.wyj.entity.po.User">
        delete from `user` where id=#{id}
    </delete>

    <select id="selectUserById" parameterType="java.lang.Integer" resultType="com.wyj.entity.po.User">
        select id,name,sex,age,address,phone from `user` where id=#{id}
    </select>

    <select id="selectUserList" resultType="com.wyj.entity.po.User">
        select id,name,sex,age,address,phone from `user`
     </select>
</mapper>

application.properties:

#tomcat port
server.port=8080
#datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://188.131.247.26:3306/springboot-tkmybatis01?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
#logging
logging.level.com.wyj.mapper:debug
#TkMybatis
mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis.type-aliases-package=com.wyj.entity

User:

@Data
@Entity
public class User implements Serializable {
    @Id
    @KeySql(useGeneratedKeys = true)
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private String address;
    private String phone;
}

SpringbootTkmybatisApplication:

@SpringBootApplication
@MapperScan("com.wyj.mapper")//tkmybatis的注解
public class SpringbootTkmybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootTkmybatisApplication.class, args);
    }
}
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值