本例所用环境:
SpringBoot MySQL MyBatis jdk1.8 Maven
首先我们先创建一个SpringBoot 项目。
数据库连接配置 1 2 3 4 5 ##数据库连接配置(部署到哪台,对应的ip需修改) spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?connectTimeout=1000&useSSL=false&useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver=com.mysql.jdbc.Driver 数据库中的数据
环境配好之后,下面分别介绍一下通过注解或者通过xml映射的形式这两种方法来使用MyBatis。
通过xml映射的形式 测试Bean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 package com.example.demo.model; public class User { private int id; private String name; private String sex; private int age; public User() { } public User(String name, String sex, int age) { this.name = name; this.sex = sex; this.age = age; } public User(int id, String name, String sex, int age) { this.id = id; this.name = name; this.sex = sex; this.age = 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
XML形式的具体操作 将mapper定义为接口,只定义方法。具体的实现在同名的xml文件中。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.example.demo.mapper; import com.example.demo.model.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface UserMapper { User getByName(@Param("name") String name); boolean insert(User user); boolean update(@Param("name") String name, @Param("sex") String sex, @Param("age") int age); void delete(@Param("name") String name); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Service服务 1 2 3 4 5 6 7 8 9 10 11 12 13 package com.example.demo.service; import com.example.demo.model.User; public interface UserService { User getUserByName(String name); boolean addUser(User user); boolean updateUser(String name, String sex, int age); void deleteUser(String name); } Service服务的实现类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package com.example.demo.service.impl; import com.example.demo.mapper.UserMapper; import com.example.demo.model.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService{ @Autowired UserMapper userMapper; @Override public User getUserByName(String name) { User user = userMapper.getByName(name); if (null != user){ return user; } return null; } @Override public boolean addUser(User user) { return userMapper.insert(user); } @Override public boolean updateUser(String name, String sex, int age) { return userMapper.update(name, sex, age); } @Override public void deleteUser(String name) { userMapper.delete(name); } } 测试接口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.example.demo.controller; import com.example.demo.model.User; import com.example.demo.service.UserService; 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.RestController; @RestController public class UserController { @Autowired UserService userService; @RequestMapping(value = "/index", method = RequestMethod.GET) public String index(){ User user = userService.getUserByName("gyl"); return user.getName()+"--"+user.getSex()+"--"+user.getAge(); } } 如果一切顺利,即将输入localhost:8080/index 你将看到如下内容
通过注解的方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.example.demo.mapper; import com.example.demo.model.User; import org.apache.ibatis.annotations.*; @Mapper public interface UserMapper { @Select("select * from TB_USER where NAME = #{name}") User getByName(@Param("name") String name); @Insert("insert into TB_USER(NAME, SEX, AGE) values(#{name}, #{sex}, #{age})") boolean insert(User user); @Update("update TB_USER set SEX=#{sex}, AGE=#{age} where NAME=#{name}") boolean update(@Param("name") String name, @Param("sex") String sex, @Param("age") int age); @Delete("delete from TB_USER where NAME = #{name}") void delete(@Param("name") String name); } 如果一切顺利,即将输入localhost:8080/index 你将看到如下内容