标签:mapper row result -name git for table 1.0 文章
从最开始的SSH(Struts+Spring+Hibernate),到后来的SMM(SpringMVC+Spring+MyBatis),到目前的S(SpringBoot),随着框架的不断更新换代,也为我们广大的程序猿提供了更多的方便,一起搭建一个从控制层到持久层的项目可能需要一两天的时间,但是采用SpringBoot的方式,我们可能只需要10分钟就能轻松完成一个web项目的搭建,下面我们介绍一下SpringBoot2.0整合MyBatis的方法
一、新建一个项目,引入相关依赖
<!-- 单元测试依赖 start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 单元测试依赖 end -->
<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>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
加粗的为改项目主要引入的包依赖
二、新建一个实体类
public class User {
private Long id;
private String name;//姓名
private Integer age;//年龄
。。。
}
二、新建一个Dao层
public interface UserDao {
int addUser(User user);
int deleteUserById(Long id);
int updateUserById(User user);
User queryUserById(Long id);
List<User> queryUserList();
}
三、新建MyBatis的sql操作文件
<?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.somta.springboot.dao.UserDao" >
<!-- Result Map-->
<resultMap id="BaseResultMap" type="com.somta.springboot.pojo.User" >
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
</resultMap>
<!-- th_role_user table all fields -->
<sql id="Base_Column_List" >
id, name, age
</sql>
<insert id="addUser" parameterType="com.somta.springboot.pojo.User" >
insert into t_user (id, name, age)
values (#{id},#{name},#{age});
</insert>
<delete id="deleteUserById" parameterType="java.lang.Long">
delete from t_user where id=#{id}
</delete>
<update id="updateUserById" parameterType="com.somta.springboot.pojo.User" >
update t_user set
<trim suffixOverrides="," >
<if test="id != null and id != ‘‘">
id=#{id},
</if>
<if test="name != null and name != ‘‘">
name=#{name},
</if>
<if test="age != null and age != ‘‘">
age=#{age},
</if>
</trim> where id=#{id}
</update>
<select id="queryUserById" resultMap="BaseResultMap" parameterType="java.lang.Long">
select <include refid="Base_Column_List" />
from t_user where id = #{id}
</select>
<select id="queryUserList" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from t_user
</select>
</mapper>
四、修改相关配置文件和MyBatis配置
@SpringBootApplication
@MapperScan("com.somta.springboot.dao")
public class Application {
public static void main(String [] args) {
SpringApplication.run(Application.class, args);
}
}
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
mybatis:
mapper-locations: classpath*:mybatis/**/Mysql_*Mapper.xml
通过@MapperScan("com.somta.springboot.dao")指定Dao层的包路径,通过mapper-locations配置了MyBatis文件的扫描路径,达到对应映射的效果
五、编写单元测试类
/**
*
* @author 明天的地平线
*
* SpringBoot 之前的版本配置单元测试类时使用 @SpringApplicationConfiguration(Application.class)
* SpringBoot 1.5.9 改用@SpringBootTest(classes = Application.class)
*
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class MyBatisTest {
@Autowired
private UserDao userDao;
/**
* 新增用户
* @throws Exception
*/
@Test
public void testAddUser() throws Exception {
User user = new User();
user.setId(889L);
user.setName("zhangsan");
user.setAge(12);
userDao.addUser(user);
}
/**
* 删除用户
* @throws Exception
*/
@Test
public void testDelUser() throws Exception {
userDao.deleteUserById(889L);
}
/**
* 修改用户信息
* @throws Exception
*/
@Test
public void testUpdUser() throws Exception {
User user = new User();
user.setId(2L);
user.setName("zhangsan99");
user.setAge(122);
userDao.updateUserById(user);
}
/**
* 查询用户
* @throws Exception
*/
@Test
public void testQueryUser() throws Exception {
User user = userDao.queryUserById(2L);
System.out.println(user.getName());
}
/**
* 查询所有用户
* @throws Exception
*/
@Test
public void testQueryUserList() throws Exception {
List<User> list = userDao.queryUserList();
for (User user : list) {
System.out.println(user.getName());
}
}
}
Git代码地址:https://gitee.com/Somta/SpringBoot/tree/master/SpringBoot-mybatis
原文地址:http://somta.com.cn/#/blog/view/b8333bea30ca4500abb01e470121ba14
---------------------
作者:明天的地平线
来源:CSDN
原文:https://blog.csdn.net/husong_/article/details/79755876
版权声明:本文为博主原创文章,转载请附上博文链接!
标签:mapper row result -name git for table 1.0 文章
原文地址:https://www.cnblogs.com/telwanggs/p/10786267.html