springboot整合mybatis(XML)
1.结构图:
2.pom.xml:


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.example springboot-mybatis 0.0.1-SNAPSHOT jar springboot-mybatis Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test com.alibaba druid 1.1.0 org.springframework.boot spring-boot-maven-plugin
3.application.yml:


#公共配置与profiles选择无关 mybatis: typeAliasesPackage: com.demo.entity mapperLocations: classpath:mapper/*.xml #开发配置 spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: root driver-class-name: com.mysql.jdbc.Driver # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource
4.sql:


CREATE TABLE `user_t` ( `id` int(10) NOT NULL AUTO_INCREMENT, `user_name` varchar(10) DEFAULT NULL, `password` varchar(10) DEFAULT NULL, `age` int(10) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=896 DEFAULT CHARSET=utf8
5.UserMapper:


id, user_name, password, age delete from user_t where id = #{id,jdbcType=INTEGER} insert into user_t (id, user_name, password, age) values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}) insert into user_t <if test="id != null" > id, if> <if test="userName != null" > user_name, if> <if test="password != null" > password, if> <if test="age != null" > age, if> <if test="id != null" > #{id,jdbcType=INTEGER}, if> <if test="userName != null" > #{userName,jdbcType=VARCHAR}, if> <if test="password != null" > #{password,jdbcType=VARCHAR}, if> <if test="age != null" > #{age,jdbcType=INTEGER}, if> update user_t <if test="userName != null" > user_name = #{userName,jdbcType=VARCHAR}, if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, if> <if test="age != null" > age = #{age,jdbcType=INTEGER}, if> where id = #{id,jdbcType=INTEGER}update user_t set user_name = #{userName,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER}
6.User:


package com.demo.entity; public class User { private Integer id; private String userName; private String password; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", age=" + age + '}'; } }
7.UserDao:


package com.demo.dao; import com.demo.entity.User; import java.util.ArrayList; public interface UserDao { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); ArrayListselectAll(); }
8.UserService:


package com.demo.service; import com.demo.entity.User; import java.util.ArrayList; public interface UserService { public User getUserById(int userId); boolean addUser(User record); public Integer update(User record); ArrayListselectAll(); boolean insert(User record); public Integer delete(int id); }
9.UserServiceImpl:


package com.demo.service; import com.demo.dao.UserDao; import com.demo.entity.User; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; @Service("UserService") public class UserServiceImpl implements UserService { @Resource private UserDao userDao; public User getUserById(int userId) { return userDao.selectByPrimaryKey(userId); } public boolean addUser(User record){ boolean result = false; try { userDao.insertSelective(record); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public Integer update(User record) { int result = 0; try{ result =userDao.updateByPrimaryKeySelective(record); } catch (Exception e) { e.printStackTrace(); } return result; } public ArrayListselectAll() { return userDao.selectAll(); } public boolean insert(User record){ boolean result = false; try { userDao.insert(record); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public Integer delete(int id) { int result = 0; try { result = userDao.deleteByPrimaryKey(id); } catch (Exception e) { e.printStackTrace(); } return result; } }
10.UserController:


package com.demo.controller; import com.demo.entity.User; import com.demo.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; @Controller @RequestMapping("/user") public class UserController { private Logger log = LoggerFactory.getLogger(UserController.class.getName()); @Resource private UserService userService; @RequestMapping("/showUser") @ResponseBody public User toIndex(HttpServletRequest request, HttpServletResponse response){ int userId = Integer.parseInt(request.getParameter("id")); User user = this.userService.getUserById(userId); return user; } @RequestMapping("/addUser") @ResponseBody public boolean toShowUser(HttpServletRequest request, HttpServletResponse response){ int userId = Integer.parseInt(request.getParameter("id")); int age = Integer.parseInt(request.getParameter("age")); String userName = request.getParameter("userName"); String password = request.getParameter("password"); User user = new User(); user.setUserName(userName); user.setAge(age); user.setId(userId); user.setPassword(password); boolean fag = this.userService.addUser(user); return fag; } @RequestMapping("/updateUser") @ResponseBody public Integer toUpdate(HttpServletRequest request, HttpServletResponse response){ int userId = Integer.parseInt(request.getParameter("id")); String password = request.getParameter("password"); User user = new User(); user.setId(userId); user.setPassword(password); int fag = this.userService.update(user); return fag; } @RequestMapping("/selectall") @ResponseBody public ArrayListselectAll(HttpServletRequest request, HttpServletResponse response){ ArrayList user = this.userService.selectAll(); log.info(user.toString()); return user; } @RequestMapping("/addUserSome") @ResponseBody public boolean toShowUserSome(HttpServletRequest request, HttpServletResponse response){ int userId = Integer.parseInt(request.getParameter("id")); int age = Integer.parseInt(request.getParameter("age")); String userName = request.getParameter("userName"); String password = request.getParameter("password"); User user = new User(); user.setUserName(userName); user.setAge(age); user.setId(userId); user.setPassword(password); boolean fag = this.userService.insert(user); return fag; } @RequestMapping("/deleteUser") @ResponseBody public String delete(HttpServletRequest request, HttpServletResponse response){ int userId = Integer.parseInt(request.getParameter("id")); int count = this.userService.delete(userId); if(count >0 ) { return "成功删除" + count + "条信息!"; }else { return "删除失败!"; } } }
11.DemoApplication:


package com.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.demo.dao") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
12.访问url:
http://localhost:8080/user/showUser?id=11 : {"id":11,"userName":"程序员","password":"yiqq","age":22}
参考:Spring boot+Mybatis整合