本文共 2662 字,大约阅读时间需要 8 分钟。
pom.xml
中添加依赖:4.0.0 cn.et SpringBoot_MyBatis 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 org.apache.tomcat.embed tomcat-embed-jasper org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java runtime com.alibaba druid 1.1.5
application.properties
中增加配置:#配置数据库四要素spring.datasource.url=jdbc:mysql://localhost/deskspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver#指定数据源spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.filters=stat,configspring.jpa.show-sql=true#端口server.port=80#项目上下文server.context-path=/sbm spring.devtools.restart.enabled=true
package cn.et;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringBootTest { public static void main(String[] args) { SpringApplication.run(SpringBootTest.class, args); }}
controller
控制层package cn.et.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import cn.et.Dept;import cn.et.dao.SpringBootMapper;@RestControllerpublic class SpringBootController { @Autowired SpringBootMapper mapper; @RequestMapping("/queryDept") public Listquery() { return mapper.queryDept(); }}
Dao
层,一个@Mapper
注解就搞定了package cn.et.dao;import java.util.List;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import cn.et.Dept;@Mapperpublic interface SpringBootMapper { @Select("select * from dept") public ListqueryDept(); }
转载地址:http://cnzsi.baihongyu.com/