MySQL学习(第三篇:myBatis访问)
2019-05-30 16:31
316 查看
1、Spring boot的myBatis配置
(1)在application.properties里,添加
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456
其中3306是本机mysql服务的端口号,test是要访问的mysql数据库。
(2)在Pom.xml添加如下,添加:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
2、应用层
@SpringBootApplication @MapperScan("org.sang.mapper") public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); } }
3、 Controller和Service层
(同JDBCTemplate对应部分)
4、数据操作层
@Repository public interface BookMapper { @Insert("insert ignore into book(id,name,author) values(#{id}, #{name}, #{author})") public int addBook(Book book); @Delete ("delete from book where id=#{id}") public int deleteBookById(@Param("id") Integer id); @Update("update book set name=#{name}, author=#{author} where id=#{id}") public int updateBookById(Book book); @Select("select * from book where id=#{id}") public Book getBookById(@Param("id") Integer id); @Select ("select * from book") public List<Book> getAllBooks(); }
注:数据库的更多操作可以参看《https://blog.csdn.net/zhangju978/article/details/90401500》
相关文章推荐
- 从.Net到Java学习第三篇——spring boot+mybatis+mysql
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
- 学习springboot笔记(四)数据访问之mybatis
- 【Mybatis学习】Mybatis学习之调用Mysql存储过程(1传入2输出)
- Spring 、SpringMVC、Mybatis、MySQL、Maven、Tomcat搭建JavaWeb项目流程---学习笔记(一)工具准备及环境配置
- Spring boot访问Mysql — MyBatis篇
- spring4 学习4 spring MVC+mybatis+Mysql
- springboot学习:springboot+mybatis+MySQL+thymeleaf(二)
- SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql
- Mysql数据库学习(四):常用Mysql C API 介绍和使用、封装一个访问Mysql数据库的类MysqlDB
- spring4 学习4 spring MVC+mybatis+Mysql
- mysql远程访问 sql删除记录 伪造浏览器post php错误处理学习 django模板改tdk
- MyBatis学习(一)、MyBatis简介与配置MyBatis+Spring+MySql
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
- 17年2月26日Java后台学习记录,Mysql 多个timestamp,xml中使用小于号,Mybatisdao接口多个参数函数错误
- Mybatis学习----逆向工程的使用(mysql)
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
- spring4 学习4 spring MVC+mybatis+Mysql