springboot之mybaits(非注解方式)
来源:互联网 发布:js文件中引入js文件 编辑:程序博客网 时间:2024/06/17 14:56
本例将采用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.Driverspring.datasource.url = jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8spring.datasource.username = rootspring.datasource.password = root##Mybatis 配置mybatis.type-aliases-package = com.springboot.domainmybatis.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
@Servicepublic 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":"我的家在山东省潍坊市。"}
阅读全文
0 0
- springboot之mybaits(非注解方式)
- springboot之整合mybatis-annotation(注解方式)
- 浅谈Mybatis之mybaits常用注解
- springboot整合redis,注解方式
- Springboot集成mybatis注解方式
- springboot mybaits mysql 整合
- SpringBoot学习之@SpringBootApplication注解
- SpringBoot入门之常用注解
- springBoot学习之注解小记
- mybaits 继承 注解
- SpringMVC非注解方式学习
- springboot @import @importResource 注解使用方式
- 5.springboot整合mybatis(注解方式)
- springboot 注解方式整合mybatis (mysql数据库)
- SpringBoot 学习记录(二): mybaits
- springboot mybaits集成分页组件
- SpringBoot配置非含main类的Controller的注解
- Mybaits注解方式 There is no getter for property named 'name' in 'class java.lang.String'
- linux提示Warning: imagettftext(): Could not find/open font错误
- OpenCV中的SVM参数优化
- Empty repository的SVN工程在Spring Cloud Config下的问题
- 覆盖 (重写)
- 使用git上传代码到码云
- springboot之mybaits(非注解方式)
- 智能交通课堂记录
- loadrunner之Windows系统资源性能常用计数器
- webrtc 发送带宽估计
- java I/O库中设计模式的应用
- Androidstudio编译c/c++jni方法
- 求一颗不含指向父节点指针的普通树中任意两个结点的最近公共祖先(O(N*N) 和 O(N) 算法)
- 子父类中成员的特点
- Spring Security(17)——基于方法的权限控制