Flask接口签名sign原理与实例代码浅析
257
2023-02-19
Spring Boot集成Mybatis的实例代码(简洁版)
概述
现在互联网应用中,大部分还是使用Mybatis来操作数据库的,本文介绍一下Spring Boot中如何集成Mybatis。
上篇介绍了Spring Boot 直接用jar运行项目的方法,需要的朋友点击查看。
创建Spring Boot工程
在 Spring Boot 开篇-创建和运行 一文中有一个小节介绍了如何使用Spring Boot的组件来创建工程。如果要集成Mybatis,只需要把mysql和Mybatis这两个组件勾选一下即可。
当然也可以不通过这种方式,直接在POM.xml文件中添加依赖也是可以的。我选择的是直接在POM.xml文件中直接添加依赖这种方式。
dependency>
数据源使用阿里的druid。完整的POM.xml文件内容如下:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 创建table CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='user信息'; 创建entity package com.springboot.entity; public class User { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } } 创建Mybatis映射文件和repo UserRepo.java package com.springboot.repo; import com.springboot.entity.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Component; import java.util.List; @Component @Mapper public interface UserRepo { int insert(User user); User selectByPrimaryKey(Long id); int updateByPrimaryKey(User user); int deleteByPrimaryKey(Long id); List } UserMapper.xml
id, name select from user where id = #{id,jdbcType=BIGINT} select from user update user `name`= #{name,jdbcType=VARCHAR}, where id = #{id,jdbcType=BIGINT} delete from user where id = #{id,jdbcType=BIGINT} insert into user name #{name,jdbcType=VARCHAR} 编写application.properties 在Spring Boot为我们生成的application.properties文件中添加如下内容: spring.datasource.name=spring_boot_study spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=xxxxxx spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.springboot.entity 单元测试 package com.springboot; import com.springboot.entity.User; import com.springboot.repo.UserRepo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SprinqPttCgRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class UserTest { @Autowired private UserRepo userRepo; @Test public void testInsert() { User user = new User(); user.setName("test2"); userRepo.insert(user); } @Test public void testUpdate() { User user = new User(); user.setId(6L); user.setName("test3"); userRepo.updateByPrimaryKey(user); } @Test public void testDelete() { userRepo.deleteByPrimaryKey(6L); } @Test public void testSelectByPrimaryKey() { User user = userRepo.selectByPrimaryKey(7L); System.out.println(user); } @Test public void testSelectAll() { List System.out.println(userList); } } 总结 以上所述是给大家介绍的Spring Boot集成Mybatis的实例代码(简洁版),希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
创建table
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='user信息';
创建entity
package com.springboot.entity;
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
创建Mybatis映射文件和repo
UserRepo.java
package com.springboot.repo;
import com.springboot.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Mapper
public interface UserRepo {
int insert(User user);
User selectByPrimaryKey(Long id);
int updateByPrimaryKey(User user);
int deleteByPrimaryKey(Long id);
List
}
UserMapper.xml
id,
name
select
from user
where id = #{id,jdbcType=BIGINT}
select
from user
update user
`name`= #{name,jdbcType=VARCHAR},
where id = #{id,jdbcType=BIGINT}
delete from user
where id = #{id,jdbcType=BIGINT}
insert into user
name
#{name,jdbcType=VARCHAR}
编写application.properties
在Spring Boot为我们生成的application.properties文件中添加如下内容:
spring.datasource.name=spring_boot_study spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=xxxxxx spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.springboot.entity
单元测试
package com.springboot;
import com.springboot.entity.User;
import com.springboot.repo.UserRepo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SprinqPttCgRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {
@Autowired
private UserRepo userRepo;
@Test
public void testInsert() {
User user = new User();
user.setName("test2");
userRepo.insert(user);
}
@Test
public void testUpdate() {
User user = new User();
user.setId(6L);
user.setName("test3");
userRepo.updateByPrimaryKey(user);
}
@Test
public void testDelete() {
userRepo.deleteByPrimaryKey(6L);
}
@Test
public void testSelectByPrimaryKey() {
User user = userRepo.selectByPrimaryKey(7L);
System.out.println(user);
}
@Test
public void testSelectAll() {
List
System.out.println(userList);
}
}
总结
以上所述是给大家介绍的Spring Boot集成Mybatis的实例代码(简洁版),希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~