整合过程介绍(开发工具:IntelliJ IDEA )
项目结构如下:
1、新建springboot maven项目,pom.xml配置如下:
4.0.0 com.xieke.test springbootmybatisdemo 0.0.1-SNAPSHOT jar springbootmybatisdemo Demo project for Spring Boot Mybatis org.springframework.boot spring-boot-starter-parent 2.0.2.RELEASE UTF-8 UTF-8 1.8 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 org.springframework.boot spring-boot-maven-plugin
2、添加application.properties文件到 resources下,配置如下:
server: port: 1314 #配置数据源 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8 username: root password: 999999 driver-class-name: com.mysql.jdbc.Driver #指定mybatis映射文件的地址 mybatis: mapper-locations: classpath:mapper/*.xml # 打印sql logging: level: com.xieke.test.dao : debug
3、SQL初始化文件table.sql,内容如下:
use test; drop table `user`; create table `user` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(25) NOT NULL, `password` varchar(25) NOT NULL, PRIMARY KEY (`id`) ); insert into `user` (`id`, `name`, `password`) values('1','xieke','999999'); insert into `user` (`id`, `name`, `password`) values('2','43434','324233'); insert into `user` (`id`, `name`, `password`) values('3','34344','343434');
4、控制层、业务层、持久层、实体类代码如下:
package com.xieke.test.controller; import com.xieke.test.entity.User; import com.xieke.test.service.UserService; 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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/show") @ResponseBody public String show(@RequestParam(value = "name")String name){ User user = userService.findUserByName(name); if(null != user) return user.getId()+"/"+user.getName()+"/"+user.getPassword(); else return "null"; } @RequestMapping(value = "/show2") @ResponseBody public String show2(@RequestParam(value = "name")String name, @RequestParam(value = "password")String password){ User user = userService.findUserByNameAndPassword(name, password); if(null != user) return user.getId()+"/"+user.getName()+"/"+user.getPassword(); else return "null"; } @RequestMapping(value = "/add") @ResponseBody public String add(){ userService.saveUser(); User user = userService.findUserByName("a123456789"); if(null != user) return user.getId()+"/"+user.getName()+"/"+user.getPassword(); else return "null"; } }
package com.xieke.test.service; import com.xieke.test.dao.UserDao; import com.xieke.test.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class UserService { @Autowired private UserDao userDao; public User findUserByName(String name){ User user = null; try{ user = userDao.findUserByName(name); } catch (Exception e){ } return user; } public User findUserByNameAndPassword(String name, String password){ User user = null; try{ User u = new User(); u.setName(name); u.setPassword(password); user = userDao.findUserByNameAndPassword(u); } catch (Exception e){ } return user; } @Transactional public void saveUser ( ){ userDao.insertUser("a123456789", "123456789"); // int n = 1/0; // userDao.insertUser("b123456789", "123456789"); } }
package com.xieke.test.dao; import com.xieke.test.entity.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @Mapper public interface UserDao { @Select("SELECT * FROM user WHERE name = #{name}") User findUserByName(@Param("name") String name); @Insert("INSERT INTO user(name, password) VALUES(#{name}, #{password})") int insertUser(@Param("name") String name, @Param("password") String password); User findUserByNameAndPassword (User user); }
package com.xieke.test.entity; public class User { private long id; private String name; private String password; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
5、启动类代码如下,启动后访问localhost:1314/user/show?name=xieke测试,详细代码请参考码云。
package com.xieke.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication @EnableTransactionManagement public class SpringbootmybatisdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootmybatisdemoApplication.class, args); } }
转载请注明出处:http://xieke90.iteye.com/blog/2423136