本篇内容主要讲解“springboot集成mybatis的实例代码”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot集成mybatis的实例代码”吧!
本节我们来详细说一下mybatis如何集成到springboot环境中
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.3.3.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
server: port: 8010 spring: application: name: demo-mybatis # 配置mysql数据库 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/lagou username: root password: root mybatis: # 指定需要加载的mapper xml文件 mapper-locations: classpath:mapping/*Mapper.xml # 指定别名所在的包 type-aliases-package: com.example.mybatis.pojo
@MapperScan
,指定需要扫描的mapper包文件路径@SpringBootApplication @MapperScan(value = "com.example.mybatis.mapper") public class DemoMybatisApplication { public static void main(String[] args) { SpringApplication.run(DemoMybatisApplication.class, args); } }
@Data @ToString public class User { private Integer id; private String username; private String password; }
@Repository
注解,表示注入spring@Repository public interface IUserMapper { List<User> getUserList(); User getUserById(Integer id); void addUser(User user); void updateUser(User user); void deleteUser(Integer id); }
<?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.example.mybatis.mapper.IUserMapper"> <resultMap id="BaseResultMap" type="com.example.mybatis.pojo.User"> <result column="id" jdbcType="INTEGER" property="id"/> <result column="userName" jdbcType="VARCHAR" property="userName"/> </resultMap> <select id="getUserList" resultType="user"> select * from users </select> <select id="getUserById" resultType="user"> select * from users where id = #{id} </select> <insert id="addUser" parameterType="user"> insert into users values(#{id}, #{username}, #{password}) </insert> <update id="updateUser" parameterType="user"> update users set username=#{username}, password= #{password} where id = #{id} </update> <delete id="deleteUser" parameterType="int"> delete from users where id = #{id} </delete> </mapper>
IUserMapper
中定义的接口是否可以正常使用@ExtendWith(SpringExtension.class) @SpringBootTest class IUserMapperTest { @Autowired private IUserMapper userMapper; @Test public void getUserList() { List<User> userList = userMapper.getUserList(); for (User user : userList) { System.out.println(user); } } @Test void addUser() { User user = new User(); user.setId(3); user.setUsername("lalala"); user.setPassword("1qaz2wx"); userMapper.addUser(user); } @Test public void getUserById() { User user = userMapper.getUserById(3); System.out.println(user); } @Test void updateUser() { User user = userMapper.getUserById(3); user.setUsername("wudi"); userMapper.updateUser(user); } @Test void deleteUser() { userMapper.deleteUser(3); } }
好了,到这里我的springboot 已经成功的集成了mybatis,接下来我们就可以使用mybatis来处理我们的持久层业务了
到此,相信大家对“springboot集成mybatis的实例代码”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。