Spring Boot 之 Mybatis

POM

 
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
       
        
        
            mysql
            mysql-connector-java
        
        
            com.alibaba
            druid
            1.0.20
        
        
            com.alibaba
            fastjson
            1.2.12
        
 

application.properties

#MYBATIS
mybatis.type-aliases-package=mybatis.domain
mybatis.mapper-locations=classpath*:/mapper/*Mapper.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30

#DATASOURCE
spring.datasource.schema=classpath:init-sql/schema.sql
spring.datasource.continueOnError=true
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost/demo-schema
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.validationQuery=SELECT 1
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

schema.sql

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `boot_user`
-- ----------------------------
DROP TABLE IF EXISTS `boot_user`;
CREATE TABLE `boot_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT NULL,
  `tel` varchar(11) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of boot_user
-- ----------------------------
INSERT INTO `boot_user` VALUES ('1', 'klay', '13799008800', '2016-06-27 00:01:39');
INSERT INTO `boot_user` VALUES ('2', 'Tome', '18988991234', '2016-06-27 00:35:28');

Controller

@Controller
public class IndexController {
    @Autowired
    private UserService userService;

    /**
     * 查询用户
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("finduser")
    public String findUser() {
        return JSON.toJSONString(userService.findAll());
    }

    /**
     * findById
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("user/{id}")
    public String findById(@PathVariable int id) {
        return JSON.toJSONString(userService.findOne(id));
    }
}

Service

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List findAll() {
        return userMapper.findAll();
    }

    @Override
    public UserInfo findOne(int id) {
        return userMapper.findOne(id);
    }

}

Mapper||Dao

@Mapper
public interface UserMapper {

    /**
     * findOne
     *
     * @param id
     * @return
     */
    @Select(value = "select *from boot_user where id=#{id}")
    UserInfo findOne(int id);

    /**
     * findAll
     *
     * @return
     */
    List findAll();
}

Mapper.xml





    
    

domain||pojo

public class UserInfo implements Serializable {
    private static final long serialVersionUID = 6519997700281088880L;

    private int id;

    private String name;

    private String tel;

    @JSONField(format = "yyyy-MM-dd")
    private Date createTime;
    
    ...getter setter

参考

代码:https://github.com/changdaye/spring-boot-demo/tree/master/spring-boot-mybatis

Spring Boot 之 Mybatis_第1张图片
image

你可能感兴趣的:(Spring Boot 之 Mybatis)