【springboot】springboot+mybatis整合实现CURD之查询《二》

1.项目结构
在这里插入图片描述
在这里插入图片描述
2.数据表t_city
在这里插入图片描述

-- ----------------------------
-- Table structure for t_city
-- ----------------------------
DROP TABLE IF EXISTS `t_city`;
CREATE TABLE `t_city`  (
  `id` int(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `provinceid` int(20) NULL DEFAULT NULL COMMENT '省份编号',
  `cityname` varchar(120) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '城市名称',
  `description` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '介绍',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of t_city
-- ----------------------------
INSERT INTO `t_city` VALUES (1, 21, '北京', '中国首都');
INSERT INTO `t_city` VALUES (2, 27, '武汉', '华中枢纽城市');

3.创建项目
在这里插入图片描述

下一步,选择对应组件;
在这里插入图片描述
4.创建项目包结构
在这里插入图片描述
City.java类

package com.springboot.ssm.entity;

import java.io.Serializable;

public class City implements Serializable{

	private static final long serialVersionUID = 1L;
	private Long id;
	private Long provinceid;
	private String cityname;
	private String description;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getProvinceid() {
		return provinceid;
	}
	public void setProvinceid(Long provinceid) {
		this.provinceid = provinceid;
	}
	public String getCityname() {
		return cityname;
	}
	public void setCityname(String cityname) {
		this.cityname = cityname;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
}

CityController.java类

package com.springboot.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.ssm.service.CityService;

@RestController
@RequestMapping(value="/city")
public class CityController {

	@Autowired
	private CityService cityService;
	
	@RequestMapping(value="/all", method = RequestMethod.GET)
	public Object findAll() {
		return cityService.findList();
	}
	
	@RequestMapping(value="hello")
	public Object hello() {
		return "hello";
	}
}

CityService.java类

package com.springboot.ssm.service;

import java.util.List;
import com.springboot.ssm.entity.City;

public interface CityService {

	public List<City> findList();
}

CityServiceImpl.java类

package com.springboot.ssm.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.ssm.dao.CityDao;
import com.springboot.ssm.entity.City;
import com.springboot.ssm.service.CityService;

@Service
public class CityServiceImpl implements CityService{

	@Autowired
	private CityDao cityDao;
	
	@Override
	public List<City> findList() {
		return cityDao.findList();
	}

}

CityDao.java类

package com.springboot.ssm.dao;

import java.util.List;
import com.springboot.ssm.entity.City;

public interface CityDao {

	public List<City> findList();
}

创建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.ssm.dao.CityDao">
	<resultMap id="BaseResultMap" type="com.springboot.ssm.entity.City">
		<result column="id" property="id" />
		<result column="provinceid" property="provinceid" />
		<result column="cityname" property="cityname" />
		<result column="description" property="description" />
	</resultMap>

	<sql id="Base_Column_List">
		id, provinceid, cityname, description
	</sql>

	<select id="findList" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from t_city
	</select>

<!-- 	<select id="findByName"  parameterType="java.lang.String" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from t_city
		where cityname = #{cityName}
	</select> -->

</mapper>

application.properties

#端口设置
server.port=8080
#MySQL数据库配置
spring.datasource.url=jdbc:mysql://192.168.231.128:3306/study?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis配置
mybatis.typeAliasesPackage=com.springboot.ssm.entity
mybatis.mapperLocations=classpath:mapper/*.xml

【注意】数据库的配置一定要对,这里配置的是远程数据库。
1.测试项目是否OK,http://localhost:8080/city/hello
在这里插入图片描述
2.如果数据库配置不对,数据库连接不上;则会出现status为500。
如下OK的情况:
在这里插入图片描述
以上就完成了查询功能,其他的增删改就简单了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值