当前位置: 首页> 健康> 知识 > 深圳建筑业协会官网_多元网站_网络营销推广方法和手段_爱论坛

深圳建筑业协会官网_多元网站_网络营销推广方法和手段_爱论坛

时间:2025/7/17 3:12:43来源:https://blog.csdn.net/daqi1983/article/details/146423442 浏览次数:2次
深圳建筑业协会官网_多元网站_网络营销推广方法和手段_爱论坛

准备工作

1、创建SpringBoot项目,以及在pom.xml中添加如下依赖

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.2</version>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>

2、新建domain子包,然后再新建两个实体类(CommentArticle)

public class Comment {private Integer id;private String content;private String author;private Integer aId;//添加set 、get、 toString}
public class Article {private Integer id;private String title;private String content;private List<Comment> commentList;//添加set 、get、 toString}

3、整合Druid:

导入Druid对应的starter:

<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.16</version>
</dependency>

在application.properties中添加数据库连接配置和第三方数据源配置

spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.initialSize=20
spring.datasource.druid.minIdle=10
spring.datasource.druid.maxActive=100

测试是否整合成功

@Autowired
private DruidDataSource dataSource;@Test
public void testDruidDataSource() {System.out.println(dataSource.getMaxActive());}

使用注解方式访问数据

1.创建mapper子包及Mapper接口文件

@Mapper
public interface CommentMapper {@Select("SELECT * FROM t_comment WHERE id =#{id}")public Comment findById(Integer id);@Insert("INSERT INTO t_comment(content,author,a_id) " +
"values (#{content},#{author},#{aId})")public int insertComment(Comment comment);@Update("UPDATE t_comment SET content=#{content} WHERE id=#{id}")public int updateComment(Comment comment);@Delete("DELETE FROM t_comment WHERE id=#{id}")public int deleteComment(Integer id);}

提示:如果接口文件过多,可以在启动类上添加@MapperScan(“接口文件所在的包名”),不必再接口文件上添加@Mapper.

2.编写测试方法进行整合测试

控制台显示SQL:

logging.level.你的Mapper包=日志等级

例如:logging.level.com.itheima.myblog.mapper=debug

@Autowired
//会报错,但不影响执行,可以在Mapper接口中添加//@Component(“commentMapper”)
private CommentMapper commentMapper;@Test
public void selectComment() {Comment comment = commentMapper.findById(1);System.out.println(comment);}

关联对象没有查询出来,如下图

原因:实体类叫aId,数据表叫a_id,名称不一致,导致映射不成功。

解决方法:因为aId按驼峰命名的,所以开启驼峰命名匹配映射

mybatis.configuration.map-underscore-to-camel-case=true

使用配置文件方式访问数据

1.创建Mapper接口文件:@Mapper

@Mapper
public interface ArticleMapper {public Article selectArticle(Integer id);public int updateArticle(Article article);}

2.创建XML映射文件:编写对应的SQL语句

创建文件夹mapper,不是资源文件夹,如下图

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.itheima.mapper.ArticleMapper"><!-- 1、查询文章详细(包括评论信息) --><select id="selectArticle" resultMap="articleWithComment">SELECT a.*,c.id c_id,c.content c_content,c.authorFROM t_article a,t_comment cWHERE a.id=c.a_id AND a.id = #{id}</select><resultMap id="articleWithComment" type="Article"><id property="id" column="id" /><result property="title" column="title" /><result property="content" column="content" /><collection property="commentList" ofType="Comment"><id property="id" column="c_id" /><result property="content" column="c_content" /><result property="author" column="author" /></collection></resultMap><!-- 2、根据文章id更新文章信息 --><update id="updateArticle" parameterType="Article">UPDATE t_article<set><if test="title !=null and title !=''">    title=#{title},</if><if test="content !=null and content !=''">content=#{content}</if></set>WHERE id=#{id}
</update></mapper>

3.在全局文件中配置XML映射文件路径以及实体类别名映射路径

#配置MyBatisxml配置文件路径

mybatis.mapper-locations=classpath:mapper/*.xml

#设置XML映射文件中的实体类别名的路径

mybatis.type-aliases-package=com.itheima.domain

4.编写测试方法进行整合测试

@Autowired
private ArticleMapper articleMapper;@Test
public void selectArticle() {Article article = articleMapper.selectArticle(1);System.out.println(article);}

动手试一试

使用XML方式访问数据,实现在数据库中增加文章和删除文章功能

关键字:深圳建筑业协会官网_多元网站_网络营销推广方法和手段_爱论坛

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: