最终项目结构目录
创建项目
新建一个Spring项目填写项目信息
选择添加的依赖
最后【Finish】完成
配置数据库源
在application.properties资源文件中添加mybatis的配置信息;项目启动时,会自动加载配置项。根据情况更改内容(数据库名称,登陆账户及密码,locations等):
#datasource
spring.datasource.tomcat.driver-class-name=com.mysql.jdbc.Driver
#mybatis
spring.datasource.url=jdbc:mysql://localhost:3306/new_schema?useUnicode=true&characterEncoding=UTF-8&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=0000
spring.datasource.tomcat.default-auto-commit=true
mybatis.typeAliasesPackage=com.example.springboot.demo.Mapper
mybatis.mapper-locations=classpath:mappers/*.xml
基于三层架构进行开发
数据库
resources文件夹(Resources Root)
基于mybatis,将SQL写入到.xml
配置文件中,添加mappers映射,userMappers.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.example.springboot.demo.Mapper.UserMapper">
<!--id对应接口中的方法,名字要一样,parameterType是方法的参数类型,
resultType是查询返回的类型,需要注意的是,这里的SQL语句后面不能加分号,变量不能加引号-->
<select id="getById" parameterType="int" resultType="com.example.springboot.demo.entity.User">
select * from user where id = #{id}
</select>
<insert id="insert" parameterType="string">
insert into user(name) values(#{name})
</insert>
<select id="getUsers" resultType="com.example.springboot.demo.entity.User">
select * from user order by #{id}
</select>
<update id="updateUser" parameterType="com.example.springboot.demo.entity.User">
update user set name=#{name} where id = #{id}
</update>
<delete id="deleteAllUsers">
delete from user
</delete>
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
</mapper>
java文件夹(Sources Root)
创建一个实体类User(entity文件夹下),用于接收数据的对象
package com.example.springboot.demo.entity;
public class User {
private String name;
private int id;
public User(){}
public User(int id,String name){
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
}
Mapper用于创建数据接口,UserMapper接口
package com.example.springboot.demo.Mapper;
import com.example.springboot.demo.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
User getById(int id);
public boolean insert(String name);
public List<User> getUsers();
public boolean updateUser(User user);
public boolean deleteUser(int id);
public boolean deleteAllUsers();
}
Service实现,UserService实现类。(很多教程中加了接口,但是我觉得我不需要就没有增加。)
package com.example.springboot.demo.service;
import com.example.springboot.demo.Mapper.UserMapper;
import com.example.springboot.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getById(int id){
return userMapper.getById(id);
}
}
Control(这里使用了Restful风格)
package com.example.springboot.demo.controller;
import com.example.springboot.demo.entity.User;
import com.example.springboot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/index") //在类上使用RequestMapping,里面设置的value就是方法的父路径
public class Controller {
@Autowired
private UserService userService;
@GetMapping("/hi") //如果方法上的RequestMapping没有value,则此方法默认被父路径调用
public String index(){
return "hello spring boot";
}
@RequestMapping("/users/{userId}")
public User getUser(@PathVariable("userId") int userId){
User user = userService.getById(userId);
return user;
}
}
DemoApplication主开关
package com.example.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan("com.example.springboot.demo.Mapper")
@EnableCaching
@ComponentScan (basePackages = {"com.example.springboot.demo.service","com.example.springboot.demo.Mapper","com.example.springboot.demo.controller"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
配置成功
不出意外的话,在浏览器中可以看到项目搭建成功了。
http://localhost:8080/index/hi
http://localhost:8080/index/users/2