1.pom文件添加依赖
<!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.0</version> </dependency>
2.在application.yml中配置数据库连接信息
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8 type: com.zaxxer.hikari.HikariDataSource username: root password: 123456 mybatis: mapper-locations: classpath:mapper/*mapper.xml #读取mapper配置 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句 logging: level: root: DEBUG #配置日志级别 org.springframework: DEBUG org.mybatis: DEBUG
3.启动类添加注解,指定接口扫描类
@MapperScan("com.zp.demozp.dao")
package com.zp.demozp; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Repository; //指定mybatis接口扫描类 //@MapperScan(basePackages = "com.zp.demozp.dao",annotationClass = Repository.class) @MapperScan("com.zp.demozp.dao") @SpringBootApplication public class DemozpApplication { public static void main(String[] args) { System.out.println("hello world!"); SpringApplication.run(DemozpApplication.class, args); } }
4.代码编写
目录结构
controller代码:
1 package com.zp.demozp.controller; 2 3 import com.zp.demozp.domain.UserDomain; 4 import com.zp.demozp.service.UserService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 7 import org.springframework.web.bind.annotation.*; 8 9 10 @RestController 11 @EnableAutoConfiguration 12 @RequestMapping("/user") 13 public class UserController { 14 15 @Autowired 16 private UserService userService; 17 18 /** 19 * 根据id获取信息 20 * @param id 21 * @return 22 */ 23 @RequestMapping(value = "/getUserById",method = RequestMethod.GET) 24 public String getUserById(@RequestParam int id){ 25 return userService.getUserById(id); 26 } 27 28 /** 29 * 添加用户 30 * @param userDomain 31 * @return 32 */ 33 @RequestMapping(value = "/add",method = RequestMethod.POST) 34 public int addUser(@RequestBody UserDomain userDomain){ 35 return userService.addUser(userDomain); 36 } 37 }
service代码:
1 package com.zp.demozp.service; 2 3 import com.zp.demozp.domain.UserDomain; 4 5 6 public interface UserService{ 7 public String getUserById(int id); 8 9 public int addUser(UserDomain userDomain); 10 }
serviceImpl代码:
package com.zp.demozp.service.impl; import com.zp.demozp.dao.UserDao; import com.zp.demozp.domain.UserDomain; import com.zp.demozp.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; /** * * @param id * @return */ @Override public String getUserById(int id) { UserDomain user = userDao.getUserById(id); return user.getName(); } @Override public int addUser(UserDomain userDomain) { return userDao.insertUser(userDomain); } }
domain代码:
package com.zp.demozp.domain; public class UserDomain { private int id; //id private String name; //姓名 private int age; //年龄 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } }
dao代码:
package com.zp.demozp.dao; import com.zp.demozp.domain.UserDomain; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; //使用maper和repisitory都可以 //@Repository @Mapper public interface UserDao { /** * 添加一条用户信息 * @param userDomain * @return */ public int insertUser(UserDomain userDomain); /** * 根据id获取用户信息 * @param id * @return */ public UserDomain getUserById(int id); }
mappwer文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.zp.demozp.dao.UserDao"> <!--根据id获取用户信息--> <select id="getUserById" resultType="com.zp.demozp.domain.UserDomain"> select * from users where id = #{id} </select> <!--添加一条用户信息--> <insert id="insertUser"> insert into users(name,age) values(#{name},#{age}) </insert> </mapper>
5.测试
添加:
查询: