SpringBoot整合Mybatis
2018-06-18 01:55:09来源:未知 阅读 ()
1.创建一个SpringBoot项目,参考:http://www.cnblogs.com/i-tao/p/8878562.html
2.创建项目目录结构
3.整合Mybatis
3.1、在pom.xml文件里添加mysql连接驱动和mybatis依赖
<!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
3.2、application.properties里配置mapper位置,config位置,数据库信息等
#springboot整合mybatis mybatis.mapper-locations=classpath:mapper/*Mapper.xml mybatis.config-location=classpath:config/sqlMapConfig.xml #mybatis定义别名 mybatis.type-aliases-package=com.tao.springboot.model #数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/db_mybatis spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root
3.3、创建User.java,UserMapper.java,UserService.java,UserServiceImpl.java,UserAction.java
com.tao.springboot.model创建User.java
package com.tao.springboot.model; public class User { private Integer id; private String user_name; private String password; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
com.tao.springboot.mapper创建UserMapper.java
package com.tao.springboot.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.tao.springboot.model.User; @Mapper public interface UserMapper { public List<User> findAll(); }
com.tao.springboot.service创建UserService.java
package com.tao.springboot.service; import java.util.List; import com.tao.springboot.model.User; public interface UserService { public List<User> findAll(); }
com.tao.springboot.service.impl创建UserServiceImpl.java实现UserService接口
package com.tao.springboot.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.tao.springboot.mapper.UserMapper; import com.tao.springboot.model.User; import com.tao.springboot.service.UserService; @Service public class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; @Override public List<User> findAll() { // TODO Auto-generated method stub return userMapper.findAll(); } }
com.tao.springboot.action创建UserAction.java
package com.tao.springboot.action; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.tao.springboot.model.User; import com.tao.springboot.service.UserService; @RestController @EnableAutoConfiguration @RequestMapping(value = "/User") public class UserAction { @Autowired private UserService userService; @RequestMapping(value = "/findAll") public List<User> findAll(){ return userService.findAll(); } }
3.4、创建SqlMapConfig.xml和UserMapper.xml
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
UserMapper.xml
<?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.tao.springboot.mapper.UserMapper"> <select id="findAll" resultType="user"> select * from user </select> </mapper>
3.5、App.java运行启动
浏览器访问:http://localhost:8080/User/findAll
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- MyBatis中的$和#,用不好,准备走人! 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
- nacos~配置中心功能~springboot的支持 2020-06-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash