您的位置:首页 > 数据库 > MySQL

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》

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: