您好,登錄后才能下訂單哦!
本篇內容主要講解“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。