Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate mybatis xml in springboot

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

本文小编为大家详细介绍"怎么在springboot中集成mybatis xml方式",内容详细,步骤清晰,细节处理妥当,希望这篇"怎么在springboot中集成mybatis xml方式"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

springboot集成mybatis

1,添加pom引用

org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java

2 application.properties

mybatis.config-locations=classpath:mybatis/mybatis-config.xmlmybatis.mapper-locations=classpath:mybatis/mapper/*.xmlmybatis.type-aliases-package=com.kerry.model spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password = 123456

3 在resource目录下创建mybatis目录并创建mybatis-config.xml文件

mybatis目录下创建mapper目录存放mapper类接口文件

package com.kerry.mapper; import java.util.List; import com.kerry.model.User; public interface UserMapper { List getAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }

model类文件

package com.kerry.mapper; import java.util.List;import com.kerry.model.User; public interface UserMapper { List getAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id);}

userMapper.xml

id, name, age, address SELECT FROM user SELECT FROM user WHERE id = #{id} INSERT INTO user (id,name,age,address) VALUES (#{id},#{name}, #{age}, #{address}) UPDATE user SET name = #{name}, age = #{age}, address = #{address} WHERE id = #{id} DELETE FROM user WHERE id =#{id}

controller:

package com.kerry.web;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; import com.kerry.model.User;import com.kerry.mapper.UserMapper; @RestControllerpublic class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/getUsers") public List getUsers() { List users=userMapper.getAll(); return users; } @RequestMapping("/getUser") public User getUser(Integer id) { User user=userMapper.getOne(id); return user; } @RequestMapping("/add") public void save(User user) { userMapper.insert(user); } @RequestMapping(value="update") public void update(User user) { userMapper.update(user); } @RequestMapping(value="/delete/{id}") public void delete(@PathVariable("id") Integer id) { userMapper.delete(id); } }

最后在启动类上加上 扫描maper interface注解

@SpringBootApplication@MapperScan("com.kerry.mapper")public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}

或者在每个XXMapper类上加上@mapper注解也行 二选一即可

比如

package com.kerry.mapper; import java.util.List;import org.apache.ibatis.annotations.Mapper;import com.kerry.model.User; @Mapperpublic interface UserMapper { List getAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }

个人建议使用直接在启动类上application上@MapperScan方便,一次搞定,不必每写一个mapper类都加上@mapper注解

附上项目结构目录 选中的类以及文件

Read here, this article "how to integrate mybatis xml mode in springboot" article has been introduced, want to master the knowledge points of this article also need to be used by yourself to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report