springboot之mybaits(非注解方式)
本例将采用maven管理,代码托管在github上,地址:https://github.com/wolf909867753/springboot。1。创建maven-module,properties,并在pom.xml中添加springboot依赖project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/
·
本例将采用maven管理,代码托管在github上,地址:https://github.com/wolf909867753/springboot。
1。创建maven-module,mybaits,并在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"> <!-- Spring Boot 启动父依赖 --> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>1.5.1.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>mybaits</artifactId> <packaging>war</packaging> <name>Springboot 整合 Mybatis 的完整 Web 案例</name> <url>http://maven.apache.org</url> <properties> <mybatis-spring-boot>1.2.0</mybatis-spring-boot> <mysql-connector>5.1.39</mysql-connector> </properties> <dependencies> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 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> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>mybaits</finalName> </build> </project>
2.创建表,参考springboot\mybaits\sql.sql
CREATE DATABASE springbootdb; use springbootdb; CREATE TABLE `city` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市编号', `province_id` int(10) unsigned NOT NULL COMMENT '省份编号', `city_name` varchar(25) DEFAULT NULL COMMENT '城市名称', `description` varchar(25) DEFAULT NULL COMMENT '描述', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; INSERT city VALUES (1 ,1,'潍坊市','我的家在山东省潍坊市。');
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 ##Mybatis 配置 mybatis.type-aliases-package = com.springboot.domain mybatis.mapper-locations = classpath:mapper/*.xml #mybatis.config = mybatis 配置文件名称 #mybatis.mapperLocations = mapper xml 文件地址 #mybatis.typeAliasesPackage = 实体类包路径 #mybatis.typeHandlersPackage = type handlers 处理器包路径 #mybatis.check-config-location = 检查 mybatis 配置是否存在,一般命名为 mybatis-config.xml #mybatis.executorType = 执行模式。默认是 SIMPLE
目录如下:
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
public interface CityDao { /** * 根据城市名称,查询城市信息 * * @param cityName 城市名 */ public City queryByName(@Param("cityName") String cityName); }
##############mapper配置文件######################
resources\mapper\CityMapper.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="com.springboot.dao.CityDao"> <resultMap id="BaseResultMap" type="com.springboot.domain.City"> <result column="id" property="id" /> <result column="province_id" property="provinceId" /> <result column="city_name" property="cityName" /> <result column="description" property="description" /> </resultMap> <parameterMap id="City" type="com.springboot.domain.City"/> <sql id="Base_Column_List"> id, province_id, city_name, description </sql> <select id="queryByName" resultMap="BaseResultMap" parameterType="java.lang.String"> select <include refid="Base_Column_List" /> from city where city_name = #{cityName} </select> </mapper>
##############mapper配置文件######################
com\springboot\service\ICityService.java
public interface ICityService { /** * 根据城市名称,查询城市信息 * @param cityName */ public City queryCityByName(String cityName); }
com\springboot\service\impl\CityServiceImpl.java
@Service public class CityServiceImpl implements ICityService { @Autowired private CityDao cityDao; @Override public City queryCityByName(String cityName) { return this.cityDao.queryByName(cityName); } }
com\springboot\controller\CityController.java
@RestController @RequestMapping("city/") public class CityController { @Autowired private ICityService cityService; @RequestMapping("query/{name}") public City queryByName(@PathVariable("name") String name){ return this.cityService.queryCityByName(name); } // @RequestMapping(value = "/api/city", method = RequestMethod.GET) // public City findOneCity(@RequestParam(value = "cityName", required = true) String cityName) { // return cityService.findCityByName(cityName); // } @RequestMapping(value = "query",method = RequestMethod.POST) public City findByName(String name){ System.out.println(">>>>>>>>>>>name:"+name); return this.cityService.queryCityByName(name); } }
com\springboot\Application.java
@SpringBootApplication @MapperScan("com.springboot.dao") public class Application /*implements ApplicationRunner*/{ // @Override // public void run(ApplicationArguments applicationArguments) throws Exception { // System.out.println(">>>>>>>>>>>>>>>>>>>>>>ApplicationRunner start"); // } public static void main(String[] args) { System.out.println(">>>>>>>>>>>>>>>>>>>>>>Application start"); SpringApplication.run(Application.class,args); } }
4.运行Application.java的main方法或者发布到tomcat,
访问http://127.0.0.1:8080/city/query/%E6%BD%8D%E5%9D%8A%E5%B8%82,
输出
{"id":1,"provinceId":1,"cityName":"潍坊市","description":"我的家在山东省潍坊市。"}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)