eclipse下整合springboot和mybatis的方法步骤

网友投稿 322 2023-01-12


eclipse下整合springboot和mybatis的方法步骤

1.新建maven项目

先新建一个maven项目,勾选上creat a simple project,填写groupid,artifactid

2.建立项目结构

3.添加依赖

org.springframework.boot

spring-boot-starter-parent

2.0.3.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

junit

junit

org.springframework.boot

spring-boot-maven-plugin

4.代码编写

在包的最外层添加启动类

PwKttZaz

package com.lee.test;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication

@EnableCaching

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

实体类

package com.lee.test.pojo;

import org.springframework.stereotype.Component;

@Component

public class User {

private int id;

private String name;

private String telephone;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getTelephone() {

return telephone;

}

public void setTelephone(String telephone) {

this.telephone = telephone;

}

}

mapper接口

package com.lee.test.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.lee.test.pojo.User;

@Mapper

public interface UserMapper {

List getUser(int id);

}

service接口

package com.lee.test.service;

import java.util.List;

import com.lee.test.pojo.User;

public interface UserService {

public List getUser(int id);

}

service接口实现

package com.lee.test.service;

import java.util.List;

import com.lee.test.pojo.User;

public interface UserService {

public List getUser(int id);

}

controller层

package com.lee.test.controller;

import java.util.List;

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

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

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

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

import com.lee.test.pojo.User;

import com.lee.test.service.UserService;

@RestController

public class UserController {

@Autowired

private UserService userService;

@RequestMapping("/getUser")

public List getUser(@RequestParam("id") int id) {

return userService.getUser(id);

}

}

还有mapper.xml的实现

select * from t_user where id = #{id}

最后是一些配置在application.properties中

spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=root

mybatis.mapper-locations: classpath:mapper/*.xml


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java简单实现数组中的逆序对
下一篇:浅谈Java引用和Threadlocal的那些事
相关文章

 发表评论

暂时没有评论,来抢沙发吧~