SpringBoot + mybatis + mysql 默认配置与手动配置步骤对比一(默认配置)

前提环境已搭建,数据库表已创建。(Eclipse + SpringBoot + mybatis + mysql)

1.创建工程

File->new->other->Spring Boot -> Spring Starter Project ->选择jdk等信息  ->  点击Web,选择Web,然后点击SQL,选择JPA、Mybatis、MYSQL,点击next,Finish。

2.开始编码

2.1修改application.properties

spring.datasource.url=

spring.datasource.username=

spring.datasource.password=

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

2.2创建实体类

package com.lele.demo.entity;

import java.util.Date;

public class User {

     private Integer PUB_USER_ID;

     private String LOGIN_NAME;

     private String PASSWORD;

    //...

     public Integer getPubUserId() {

           return PUB_USER_ID;

     }

     public void setPubUserId(Integer PUB_USER_ID) {

           this.PUB_USER_ID = PUB_USER_ID;

     }

//...省略

}

2.3修改pom.xml(看实际情况修改)

2.4创建Mapper(注意Interface文件是接口,不是Class--类中方发要有实现内容的, 接口可以只有方法名不用写方法的具体实现)

package com.lele.demo.DAO;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.Param;

import org.apache.ibatis.annotations.Select;

@Mapper

public interface UserMapper {

      /** * 根据主键查询单个 * * @param id * @return */

     @Select("select TEL from pisp_pub_user where PUB_USER_ID=#{id}")

     List<String> selectById(@Param("id") Long id);


}

2.5创建Dao

package com.lele.demo.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import com.lele.demo.mapper.UserMapper;

import com.lele.demo.model.User;

@Repository

public class UserDao {

     @Autowired

     private UserMapper userMapper;


     public User selectUserById(Long id) throws DataAccessException {

           return userMapper.selectUserById(id);

     }

}

 

2.6 service接口

package com.lele.demo.service;

import java.util.List;

import com.lele.demo.entity.User;

public interface UserService {

     User selectUserById(Long id);

}

2.7 接口实现serviceImpl

package com.lele.demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.lele.demo.dao.UserDao;

import com.lele.demo.entity.User;

import com.lele.demo.service.UserService;

@Service

public class UserServiceImpl implements UserService{

@Autowired

private UserDao userDao;


     @Override

     public User selectUserById(Long id) {

           // TODO Auto-generated method stub

           return userDao.selectUserById(id);

     }


}

2.8创建Controller

package com.lele.demo.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.EnableTransactionManagement;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.lele.demo.service.UserService;

import com.lele.demo.model.User;

@EnableTransactionManagement  // 需要事务的时候加上

@RestController

public class UserController {

     @Autowired

    private UserService userService;

     @RequestMapping("/selectUserById/{id}")

    public User selectUserById(@PathVariable("id") Long id) {

        return userService.selectUserById(id);

     }

}

2.9应用入口

 

package com.lele.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringbootApplication {

     public static void main(String[] args) {

           SpringApplication.run(SpringbootApplication.class, args);

     }

}

 

完成,运行程序, 打开浏览器访问

http://127.0.0.1:8080/selectUserById/2

 

附应用目录结构:

SpringBoot + mybatis + mysql 默认配置与手动配置步骤对比一(默认配置)