1.创建一个SpringBoot项目,参考:http://www.cnblogs.com/i-tao/p/8878562.html
2.创建项目目录结构
3.整合Mybatis
3.1、在pom.xml文件里添加mysql连接驱动和mybatis依赖
org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.0 mysql mysql-connector-java
3.2、application.properties里配置mapper位置,config位置,数据库信息等
#springboot整合mybatismybatis.mapper-locations=classpath:mapper/*Mapper.xmlmybatis.config-location=classpath:config/sqlMapConfig.xml#mybatis定义别名mybatis.type-aliases-package=com.tao.springboot.model#数据库配置spring.datasource.url=jdbc:mysql://localhost:3306/db_mybatisspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.username=rootspring.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;@Mapperpublic interface UserMapper { public ListfindAll();}
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 ListfindAll();}
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;@Servicepublic class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; @Override public ListfindAll() { // 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 ListfindAll(){ return userService.findAll(); }}
3.4、创建SqlMapConfig.xml和UserMapper.xml
SqlMapConfig.xml
UserMapper.xml
3.5、App.java运行启动
浏览器访问:http://localhost:8080/User/findAll