在IDEA环境下对springboot+Mybatis的搭建

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_28582847/article/details/79930639

该项目的Demo:https://download.csdn.net/download/qq_28582847/10346928

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


展开阅读全文

没有更多推荐了,返回首页