1. 程式人生 > >在IDEA環境下對springboot+Mybatis的搭建

在IDEA環境下對springboot+Mybatis的搭建

1.新建專案






2.編寫application.properties,程式碼如下

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#頁面熱載入
spring.thymeleaf.cache = false

mybatis.configLocation=classpath:mybatis/mybatis-config.xml
mybatis.mapperLocations=classpath:mapper/*.xml

3.新建mybatis-config.xml到resource/mybatis目錄下,上述有結構圖參考

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<typeAliases>
		<typeAlias alias="Integer" type="java.lang.Integer" />
		<typeAlias alias="Long" type="java.lang.Long" />
		<typeAlias alias="HashMap" type="java.util.HashMap" />
		<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
		<typeAlias alias="ArrayList" type="java.util.ArrayList" />
		<typeAlias alias="LinkedList" type="java.util.LinkedList" />
	</typeAliases>
</configuration>

4. 新建一個UserMapper介面類

@Mapper
public interface UserMapper {

    List<User> findAll();

}

5.新建User實體類

public class User implements Serializable{

    private static final long serialVersionUID = 1L;

    private String id;

    private String name;

    private String sex;

    private String age;

    private String job;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}

6.在resources/mapper目錄下,新建mybatis對映UserMapper.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.demo.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.example.demo.model.User">
        <result column="ID" property="id"/>
        <result column="NAME" property="name"/>
        <result column="SEX" property="sex"/>
        <result column="AGE" property="age"/>
        <result column="JOB" property="job"/>
    </resultMap>

    <select id="findAll" resultMap="BaseResultMap">
        select * from user
    </select>
</mapper>

7.編寫UserService實現類

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<User> findAll(){
        return  userMapper.findAll();
    }

}

8.編寫TestController控制器

@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toTest")
    public String toTest(){
        return "index";
    }

    @ResponseBody
    @RequestMapping("/findAll")
    public List<User> findALL(){
        return  userService.findAll();
    }
}

9.啟動程式,瀏覽器訪問http://localhost:8080/test/findAll