> 文档中心 > SpringBoot+Mybatis+Maven整合操作实例

SpringBoot+Mybatis+Maven整合操作实例

关于mybatis的详细操作和要点可以参考这篇:mybatis实践方法分享

相关好文合集:

SpringBoot整合JPA项目实战

常常急需的基于MVC的信息管理系统(很全)

java比较器Comparator/Comparable怎么玩?

Java复制一个文件的内容到另一个文件的七种方法

jsp与Servlet前后端交互典型实例

基于MVC的信息管理系统(你没看错,这就是你想要的)

前后端ajax请求交互验证(通用)

SpringBoot邮件发送(抄送、密送、图片、多文件等一应俱全哦)

SpringBoot+Mybatis+Maven整合操作实例

SpringBoot+JPA+Maven/Gradle开发实例

创建项目此处不赘述了,自行熟悉一下哦。

项目结构

注意:这种项目上偶尔有个小红x,出现时在项目上右键--maven--更新项目即可。

看看配置文件(注意观察mybatis的配置及文件所在位置)

server.port=8888server.context-path=/LANW2.0mybatis.type-aliases-package=com.wl.domainspring.devtools.restart.enabled=true#mybatis.config-locations=classpath:mybatis/mybatis-config.xmlmybatis.mapper-locations=classpath:/mapper/*Mapper.xmlspring.datasource.url = jdbc:mysql://localhost:3308/lanwan2spring.datasource.username = rootspring.datasource.password = wlspring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10#static file path:js/cssspring.mvc.static-path-pattern=/static/**spring.messages.basename=i18n/messagesspring.messages.cache-seconds=3600spring.messages.encoding=UTF-8#org.hibernate.dialect.Oracle9iDialect#thymeleafspring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=LEGACYHTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/htmlspring.thymeleaf.cache=false

实体类

package com.wl.domain;public class TestUser {private int id ;private String name ;private String address ;public TestUser(String name, String address) {this.name = name;this.address = address;}public TestUser(int id, String name, String address) {this.id = id;this.name = name;this.address = address;}public TestUser(String name, String address,int id) {this.id = id;this.name = name;this.address = address;}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 getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "TestUser [userid=" + id + ", name=" + name + ", password=" + address + "]";}}

Controller

package com.wl.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.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import com.wl.domain.TestUser;import com.wl.service.TestUserService;@RestController@RequestMapping("/testuser")public class TestUserController {    @Autowired    private TestUserService testUserService;     //http://localhost:8888//LANW2.0/testuser/hello//    @RequestMapping(value = {"/hello"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)    @RequestMapping("hello")    public String hello() {    return "nihao!!!";    } //http://localhost:8888//LANW2.0/testuser/selectAll    //    @GetMapping(value="/selectAll")    @RequestMapping(value = {"/selectAll"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)    @ResponseBody    public List findAllUser(){ return testUserService.findAllUser();    } //测试插入http://localhost:8888//LANW2.0/testuser/testinsert    @RequestMapping(value = {"/testinsert"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)    @ResponseBody    public List testinsert(){    TestUser u = new TestUser("同学3","地址3"); testUserService.testInsert(u); return testUserService.findAllUser();    }}

Service及其实现类

package com.wl.service;import java.util.List;import com.wl.domain.TestUser;public interface TestUserService {List findAllUser();void testInsert(TestUser u);}

注意:Service注解在实体类上,且必须加上

package com.wl.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.wl.domain.TestUser;import com.wl.mapper.TestUserMapper;import com.wl.service.TestUserService;@Service(value = "testUserService")public class TestUserServiceImpl implements TestUserService{    @Autowired    private TestUserMapper userDao;//这里可能会报错,但是并不会影响     public List findAllUser(){return  userDao.findAllUser();    }@Overridepublic void testInsert(TestUser u) {userDao.testInsert(u);}}

Dao

package com.wl.mapper;import java.util.List;import org.apache.ibatis.annotations.Mapper;import com.wl.domain.TestUser;@Mapperpublic interface TestUserMapper {    //测试插入    public void testInsert(TestUser u);    //列表    public List findAllUser();}

userMapper接口对应的xml文件

insert into testuser(name,address) values(#{name},#{address})select * from testuser

欧了,到此所有需要的代码都为你准备好了,还等什么呢,自己试试呗~_~

老江饲料商城