本例将采用maven管理,代码托管在github上,地址:https://github.com/wolf909867753/springboot。
1。创建maven-module,mybatis-annotation,并在pom.xml中添加springboot依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId>spring-boot-starter-parentartifactId> <groupId>org.springframework.bootgroupId> <version>1.5.1.RELEASEversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>mybatis-annotationartifactId> <packaging>warpackaging> <name>mybatis-annotation Demoname> <url>http://maven.apache.orgurl> <properties> <mybatis-spring-boot>1.2.0mybatis-spring-boot> <mysql-spring-boot>5.1.32mysql-spring-boot> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>${mybatis-spring-boot}version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>${mysql-spring-boot}version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope> dependency> dependencies> <build> <finalName>mybatis-annotationfinalName> build> project>
2.工程结构
3.创建工程mybaits,添加配置文件resources\application.properties工程
## 数据源配置 spring.datasource.driver-class-name = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8 spring.datasource.username = root spring.datasource.password = root
目录如下:
com\springboot\domain\City.java
public class City implements Serializable{ /** * 城市编号 */ private Long id; /** * 省份编号 */ private Long provinceId; /** * 城市名称 */ private String cityName; /** * 描述 */ private String description;getter/setter.....
}
com\springboot\dao\CityDao.java
/** * 城市 DAO 接口类 * Created by wanglu-jf on 17/6/27. * @Mapper 标志接口为 MyBatis Mapper 接口 * @Select 是 Select 操作语句 * @Results 标志结果集,以及与库表字段的映射关系 */ @Mapper public interface CityDao { /** * 查询城市信息 */ @Select(" SELECT * FROM city WHERE id = #{id}") @Results({ @Result(property = "id", column = "id"), @Result(property = "provinceId", column = "province_id"), @Result(property = "cityName", column = "city_name"), @Result(property = "description", column = "description") }) public City queryCityById(@Param("id") int id); }
public interface ICityService { /** * 查询城市信息 */ public City queryById(int id); }
@Service public class CityServiceImpl implements ICityService { @Autowired private CityDao cityDao; /** * 查询城市信息 */ @Override public City queryById(int id) { return cityDao.queryCityById(id); } }
@RestController @RequestMapping("city/") public class CityController { @Autowired private ICityService cityService; @RequestMapping(value = "query/{id}",method = RequestMethod.GET) public City queryById(@PathVariable("id") int id){ return this.cityService.queryById(id); } }
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }