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

 

附应用目录结构:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐乐Gold

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值