1.創建一個SpringBoot項目,參考:http://www.cnblogs.com/i-tao/p/8878562.html 2.創建項目目錄結構 3.整合Mybatis 3.1、在pom.xml文件里添加mysql連接驅動和mybatis依賴 3.2、application.properti ...
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