spring-boos中引入mybatis示例

上一篇文章介绍了如何使用spring-boot搭建一个简单的web应用。这篇重点介绍如何把mybatis引入到其中。

1、pom.xml中引入:

<properties>
    <mybatis-spring-boot>1.2.0</mybatis-spring-boot>
    <mysql-connector>5.1.39</mysql-connector>
  </properties>
<!-- Spring Boot Mybatis 依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis-spring-boot}</version>
    </dependency>
    
     <!-- MySQL 连接驱动依赖 -->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>${mysql-connector}</version>
     </dependency>

整个工程结构如下图


2、entity实体类:

public class Test {
	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;
	}
}

3、dao接口:

public interface TestDao {
	/**
     * 根据名称,查询test信息
     *
     * @param name 名
     */
    Test findByName(@Param("name") String n);
}

Mybatis是基于接口编程的,我们写好接口,对应编写映射文件中的sql语句即可,非常灵活。

4、mapper.xml文件:

在resources文件夹下新建一个mapper文件夹,里面存放mybatis的mapper XML映射文件。

<?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="nc.edu.nuc.dao.mysql.TestDao">  
    <resultMap id="BaseResultMap" type="nc.edu.nuc.entity.Test">  
        <result column="id" property="id" />  
        <result column="name" property="name" />  
    </resultMap>  
  
    <parameterMap id="Test" type="nc.edu.nuc.entity.Test"/>  
  
    <sql id="Base_Column_List">  
        id,name  
    </sql>  
  
    <select id="findList" resultMap="BaseResultMap">  
        select  
        <include refid="Base_Column_List" />  
        from test  
    </select>  
  
    <select id="findByName" resultMap="BaseResultMap" parameterType="java.lang.String">  
        select  
        <include refid="Base_Column_List" />  
        from test  
        where name = #{name}  
    </select>  
      
    <insert id="save" parameterType="Test">  
        insert into test   
        (name)   
        values   
        (#{name})  
    </insert>  
      
</mapper>  

5、service类:

@Service
public class TestService{
	
	@Autowired  
	private TestDao testDao;  
	
	public Test findByName(String name) {
		return testDao.findByName(name);
	}
	
	
}

6、controller接口:

@Controller
public class TestController {
	
	@Autowired
	private TestService testService;
	
	@RequestMapping("test")
    @ResponseBody
    public String home(HttpServletRequest request) {
		String name = request.getParameter("name");
		Test findByName = testService.findByName(name);
        return findByName.toString();
    }

}

7、启动类:

@SpringBootApplication
//mapper 接口类扫描包配置
@MapperScan("nc.edu.nuc.dao.mysql")
public class App 
{
    public static void main( String[] args )
    {
    	SpringApplication.run(App.class, args);
    }
}

在启动泪伤添加一个@MapperScan的注解,也可以在每一个Dao类上添加一个@Mapper的注解。

application.properties:

##mysql配置
spring.datasource.url=jdbc:mysql://jy.ttengine.w.abc.db:1883/ttengine?useUnicode=true&characterEncoding=utf8
spring.datasource.username=ttengine
spring.datasource.password=TTengine123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

## Mybatis配置
mybatis.typeAliasesPackage=cn.edu.nuc.springbootmybatis.entity
mybatis.mapperLocations=classpath:mapper/*.xml


# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/page/
spring.mvc.view.suffix=.jsp


参考:https://github.com/JeffLi1993/springboot-learning-example

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值