MySQL学习(第三篇:myBatis访问)

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》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值