1.如何在springboot中整合Mybatis,并使用 **mapper.xml ?
一、导入相关依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.mybatis.spring.boot mybatis-spring-boot-starter mysql mysql-connector-java 复制代码 com.alibaba druid
二、编写配置文件
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复制代码
三、开始写数据库表对应的java对象,写操作数据库的接口 (同时与相应的 **mapper.xml)文件相映射 Eg:
接口://@Mapperpublic 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.xml
复制代码 insert into t_spu (spu_name,spu_headphoto,spu_detailphoto,spu_decphoto,spu_status) values (#{spuName},#{spuHeadphoto},#{spuDetailphoto},#{spuDecphoto},#{spuStatus})
四、在写一个controller访问即可