一、pom文件引入依赖
org.springframework.boot spring-boot-starter-data-jdbcorg.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-starter2.1.4 mysql mysql-connector-javaruntime junit junit4.12 test
二、application文件配置属性
mybatis: mapper-locations: classpath:mappers/*xml type-aliases-package: com.yunluo.appointment.*.model server: port: 8080 spring: application: name: appointment datasource: driver-class-name: com.mysql.cj.jdbc.Driver name: defaultDataSource password: 1234 url: jdbc:mysql://localhost:3306/dda?serverTimezone=UTC username: sun
三、启动类添加注解@MapperScan
package com.yunluo.appointment; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.yunluo.appointment.*.mapper") public class AppointmentApplication { public static void main(String[] args) { SpringApplication.run(AppointmentApplication.class, args); } }
四、Mapper.xml 文件配置
id, name, passward select from t_user where id = #{id,jdbcType=INTEGER}
五 、定义实体类
package com.yunluo.appointment.user.model; import lombok.ToString; @ToString public class User { private Integer id; private String name; private String passward; public User(Integer id, String name, String passward) { this.id = id; this.name = name; this.passward = passward; } public User() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassward() { return passward; } public void setPassward(String passward) { this.passward = passward; } }
六、定义Service接口以及其实现类
package com.yunluo.appointment.user.service; import com.yunluo.appointment.user.model.User; import org.springframework.transaction.annotation.Transactional; @Transactional public interface UserService { User selectByPrimaryKey(Integer id); }
package com.yunluo.appointment.user.service.imp; import com.yunluo.appointment.user.mapper.UserMapper; import com.yunluo.appointment.user.model.User; import com.yunluo.appointment.user.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); } }
七、定义Junit测试类
package com.yunluo.appointment; import com.yunluo.appointment.user.model.User; import com.yunluo.appointment.user.service.UserService; import org.junit.Before; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = AppointmentApplication.class) class AppointmentApplicationTests { @Test void contextLoads() { } @Autowired private UserService userService; private User user; @Before public void setUp() throws Exception { user = new User(); } @Test public void get() { user = userService.selectByPrimaryKey(1); System.out.println(user); } }
八、定义Controller类
package com.yunluo.appointment.user.controller; import com.yunluo.appointment.user.model.User; import com.yunluo.appointment.user.service.UserService; import com.yunluo.appointment.util.JsonData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; private User user; @RequestMapping("/get") public JsonData get(Integer id) { user = userService.selectByPrimaryKey(id); JsonData jsonData = new JsonData(); jsonData.setResult(user); return jsonData; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)