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的情况:
以上就完成了查询功能,其他的增删改就简单了。