SpringBoot整合Mybatis
1.创建一个SpringBoot项目,参考:http://www.cnblogs.com/i-tao/p/8878562.html
2.创建项目目录结构
3.整合Mybatis
3.1、在pom.xml文件里添加mysql连接驱动和mybatis依赖
<!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
3.2、application.properties里配置mapper位置,config位置,数据库信息等
#springboot整合mybatis mybatis.mapper-locations=classpath:mapper/*Mapper.xml mybatis.config-location=classpath:config/sqlMapConfig.xml #mybatis定义别名 mybatis.type-aliases-package=com.tao.springboot.model #数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/db_mybatis spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root
3.3、创建User.java,UserMapper.java,UserService.java,UserServiceImpl.java,UserAction.java
com.tao.springboot.model创建User.java
package com.tao.springboot.model; public class User { private Integer id; private String user_name; private String password; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
com.tao.springboot.mapper创建UserMapper.java
package com.tao.springboot.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.tao.springboot.model.User; @Mapper public interface UserMapper { public List<User> findAll(); }
com.tao.springboot.service创建UserService.java
package com.tao.springboot.service; import java.util.List; import com.tao.springboot.model.User; public interface UserService { public List<User> findAll(); }
com.tao.springboot.service.impl创建UserServiceImpl.java实现UserService接口
package com.tao.springboot.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.tao.springboot.mapper.UserMapper; import com.tao.springboot.model.User; import com.tao.springboot.service.UserService; @Service public class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; @Override public List<User> findAll() { // TODO Auto-generated method stub return userMapper.findAll(); } }
com.tao.springboot.action创建UserAction.java
package com.tao.springboot.action; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.tao.springboot.model.User; import com.tao.springboot.service.UserService; @RestController @EnableAutoConfiguration @RequestMapping(value = "/User") public class UserAction { @Autowired private UserService userService; @RequestMapping(value = "/findAll") public List<User> findAll(){ return userService.findAll(); } }
3.4、创建SqlMapConfig.xml和UserMapper.xml
SqlMapConfig.xml
<?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> </configuration>
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.tao.springboot.mapper.UserMapper"> <select id="findAll" resultType="user"> select * from user </select> </mapper>
3.5、App.java运行启动
浏览器访问:http://localhost:8080/User/findAll
相关推荐
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
liqiancao 2020-07-26
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25
牧场SZShepherd 2020-07-20