- 项目创建过程,代码在后面
- 项目目录结构
- 在application.properties文件下配置端口号,mapper路径,数据源
- mysql6.0要求连接字符串显性表示 SSL和serverTimezone
- classpath:是指resources/下面的目录
#服务的端口号
server.port=8918
#mybatis配置文件路径
mybatis.config-location = classpath:mybatisConfig/mybatisConfig.xml
#定义mapper.xml文件所在的目录
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
#定义数据源
spring.datasource.url=jdbc:mysql://localhost:3306/wddmysql?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=5258
- 在java目录com.example.demo123报下创建3个文件夹
- controller
- dao
- model
在Controller目录下创建UserController
package com.example.demo123.controller;
import com.example.demo123.dao.GetUser;
import com.example.demo123.model.User;
import com.example.demo123.model.UserNew;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class UserController {
@Autowired
GetUser getUser;
@RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
@ResponseBody
public String getHello() {
return "Hello,Word!!!";
}
@RequestMapping(value = "/sel", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
@ResponseBody
public List<UserNew> getUser() {
return getUser.getUserList();
}
@RequestMapping(value = "/selNew", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
@ResponseBody
public List<User> getUserNew() {
return getUser.selTest();
}
}
- 在dao目录下创建GetUser接口(注意是接口)
- 使用@mapper注释
package com.example.demo123.dao;
import com.example.demo123.model.User;
import com.example.demo123.model.UserNew;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface GetUser {
// @Select("select user_id,user_name from user")
public List<UserNew> getUserList();
public List<User> selTest();
}
- 在resources下创建mapper目录
- 在mapper目录下创建UserMapper.xml
- namespace是GetUser的包名+类名
<?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.example.demo123.dao.GetUser">
<resultMap id="selTestMap" type="com.example.demo123.model.User">
<id column="user_id" property="userId"></id>
</resultMap>
<select id="selTest" resultMap="selTestMap">
select user_id from user;
</select>
<select id="getUserList" resultType="com.example.demo123.model.UserNew">
select user_id,user_name from user
</select>
</mapper>
- model下创建User(属性和表的列不同名)和UserNew(同名)
package com.example.demo123.model;
public class User {
private int userId;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
package com.example.demo123.model;
public class UserNew {
private int user_id;
private String user_name;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
}
- com.example.demo123.Demo123Application是项目自己生成的main()
package com.example.demo123;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo123Application {
public static void main(String[] args) {
SpringApplication.run(Demo123Application.class, args);
}
}