1、導入相關依賴java
<!--springboot web 所需依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--springboot 整合mybatis 所需mybatis的依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- 所需的mysql數據庫驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 數據庫鏈接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
複製代碼
2、編寫配置文件mysql
spring:
datasource:
url: jdbc:mysql://localhost:3306/數據庫名?characterEncoding=utf-8&useSSL=false
username: 用戶名
password: 密碼
driver-class-name: com.mysql.jdbc.Driver
#配置mybatis 所需映射的mapper.xml 文件的位置
mybatis:
mapper-locations: classpath:mapper/*.xml
#type-aliases-package: com.fancy.account.spu.dbobject
複製代碼
3、開始寫數據庫表對應的java對象,寫操做數據庫的接口 (同時與相應的 **mapper.xml)文件相映射 Eg:web
接口:
//@Mapper
public interface TSpuDao {
void addSpu(@Param("spuName") String spuName, @Param("spuHeadphoto") String spuHeadphoto, @Param("spuDetailphoto") String spuDetailphoto, @Param("spuDecphoto") String spuDecphoto,@Param("spuStatus") int spuStatus);
}
複製代碼
對應的**mapper.xmlspring
<?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.fancy.account.spu.dao.TSpuDao">
<insert id="addSpu" parameterType="com.fancy.account.spu.dbobject.TSpu">
insert into t_spu (spu_name,spu_headphoto,spu_detailphoto,spu_decphoto,spu_status)
values (#{spuName},#{spuHeadphoto},#{spuDetailphoto},#{spuDecphoto},#{spuStatus})
</insert>
</mapper>
複製代碼
4、在寫一個controller訪問便可sql