当前位置: 首页 > wzjs >正文

网站开发者工具解读网站建设的要点是什么

网站开发者工具解读,网站建设的要点是什么,wordpress伪静态不跳转404,重庆建设工程信息网官网入渝备案在现代Java企业级应用开发中,Spring框架和MyBatis作为两个极为重要的技术栈,各自发挥着不可替代的作用。Spring框架提供了全面的基础设施支持,包括依赖注入、事务管理、AOP等核心功能,而MyBatis则是一个优秀的持久层框架&#xff…

        在现代Java企业级应用开发中,Spring框架和MyBatis作为两个极为重要的技术栈,各自发挥着不可替代的作用。Spring框架提供了全面的基础设施支持,包括依赖注入、事务管理、AOP等核心功能,而MyBatis则是一个优秀的持久层框架,以其灵活的SQL映射和简单的API著称。

将这两者整合使用,可以充分发挥各自的优势:

  • Spring的IoC容器可以管理MyBatis的组件生命周期

  • Spring的事务管理能够为MyBatis操作提供声明式事务支持

  • MyBatis的灵活性弥补了Spring Data JPA在某些复杂SQL场景下的不足

  • 简化开发通过整合可以减少大量样板代码,提高开发效率

Spring框架整合Mybatis分为以下这么几步:

  1. 创建maven模块
  2. 导入依赖坐标
  3. 创建Spring配置类
  4. 进行整合测试

1.创建模块

2. 导入依赖坐标

        <!-- Spring上下文支持 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.1.12</version></dependency><!-- Java Activation Framework API --><dependency><groupId>javax.activation</groupId><artifactId>javax.activation-api</artifactId><version>1.2.0</version></dependency><!-- MyBatis 核心库 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><!-- MyBatis 与 Spring 集成 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>3.0.3</version></dependency><!-- MySQL JDBC驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><!-- 阿里巴巴数据库连接池 druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.20</version></dependency><!-- Spring JDBC 支持 --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>6.1.12</version></dependency><!-- Spring 测试支持 --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>6.1.14</version></dependency><!-- 单元测试框架JUnit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!-- Lombok 简化Java实体类开发 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.26</version></dependency>

3. 创建核心配置类

package com.it.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
// 扫描包
@ComponentScan("com.it")
public class SpringConfig {
}

        到此,Spring框架创建成功了

4. Spring整合

4.1 整合jdbc(数据库)

        1)首先需要在jdbc.properties配置文件中配置需要连接的数据库,连接池,用户名密码

# 数据库连接URL,使用MySQL数据库,连接到本地的db1数据库
jdbc.url=jdbc:mysql:///db1
# JDBC驱动类名,使用的是com.mysql.jdbc.Driver
jdbc.driver=com.mysql.jdbc.Driver
# 数据库用户名,用于连接数据库的用户为root
jdbc.username=root
# 数据库密码,连接数据库的密码为1234
jdbc.password=1234

        2)在jdbc整合配置类中扫描配置类,拿到数据库相关信息

package com.it.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
//1.扫描配置文件
@PropertySource("jdbc.properties")
public class JdbcConfig {//    2.属性注入@Value("${jdbc.url}")private String url;@Value("${jdbc.driver}")private String driver;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;//    使用工厂实例化整合 druid/*** 创建并配置Druid数据源实例* 通过@Bean注解将数据源对象纳入Spring容器管理* @return 配置好的DruidDataSource实例*/@Beanpublic DruidDataSource getDruidDataSource() {// 实例化Druid数据源对象DruidDataSource dataSource = new DruidDataSource();// 设置数据库驱动类名dataSource.setDriverClassName(driver);// 设置数据库连接URLdataSource.setUrl(url);// 设置数据库登录用户名dataSource.setUsername(username);// 设置数据库登录密码dataSource.setPassword(password);// 返回配置完成的数据源实例return dataSource;}}

        测试一下

        至此,数据库就整合完了✌

4.2 整合Mybatis

        整合Mybatis分为两步

  • 整合核心配置文件
  • 整合映射配置文件

在MybatisConfig配置类中进行以下配置

package com.it.config;import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;import javax.sql.DataSource;@Component
public class MybatisConfig {/*** 核心配置* 配置 SqlSessionFactoryBean,用于集成 MyBatis 与 Spring* @param dataSource 自动注入的数据源* @return 配置好的 SqlSessionFactoryBean 实例*/@Beanpublic SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource) {SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();// 设置数据源ssfb.setDataSource(dataSource);return ssfb;}/*** 映射配置* Mapper 扫描配置* 配置 Mapper 接口的扫描路径,用于自动注册 Mapper 接口为 Spring Bean* @return 配置好的 MapperScannerConfigurer 实例*/@Beanpublic MapperScannerConfigurer mapperScannerConfigurer() {MapperScannerConfigurer msc = new MapperScannerConfigurer();// 设置 Mapper 接口所在的基包路径msc.setBasePackage("com.it.mapper");return msc;}}

        创建pojo、mapper进行测试

  • pojo实体类
package com.it.pojo;import lombok.Data;@Data
public class Dept {private int id;private String dname;private String loc;
}
  • mapper层
package com.it.mapper;import com.it.pojo.Dept;
import org.apache.ibatis.annotations.Select;import java.util.List;public interface DeptMapper {@Select("select * from dept")public List<Dept> findAll();
}
  • test测试
package com.it;import com.alibaba.druid.pool.DruidDataSource;
import com.it.config.SpringConfig;
import com.it.mapper.DeptMapper;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.sql.SQLException;public class Test {public static void main(String[] args) throws SQLException {AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig.class);DeptMapper deptMapper = ioc.getBean(DeptMapper.class);System.out.println(deptMapper.findAll());}
}

        成功打印✌

4.3 整合junit测试

package com.it.mapper;import com.it.config.SpringConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.junit.Assert.*;/*** spring整合junit测试* @RunWith 在测试启动的时候 加载spring容器* @ContextConfiguration 配置spring容器*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class DeptMapperTest {@Autowiredprivate DeptMapper deptMapper;@Testpublic void findAll() {System.out.println(deptMapper.findAll());}
}

       到现在Spring整合Mybatis就完成了


文章转载自:

http://eLYQi0ti.mfLqd.cn
http://I2fCjeQU.mfLqd.cn
http://OPYzlN2Y.mfLqd.cn
http://9SGX5KCA.mfLqd.cn
http://mwnVoQI4.mfLqd.cn
http://DYnm63fE.mfLqd.cn
http://EYE4Mcrt.mfLqd.cn
http://btnrdAft.mfLqd.cn
http://yHxQfsIk.mfLqd.cn
http://CjGYhkaE.mfLqd.cn
http://aj8BVEzs.mfLqd.cn
http://mFOwfXRO.mfLqd.cn
http://g3U5s4r7.mfLqd.cn
http://GCBPMlDC.mfLqd.cn
http://xuCoaS1s.mfLqd.cn
http://OPDDdAD5.mfLqd.cn
http://trpyz0ry.mfLqd.cn
http://GeXblknx.mfLqd.cn
http://96Y1HzAX.mfLqd.cn
http://xI1FszeJ.mfLqd.cn
http://Lk6mglWQ.mfLqd.cn
http://u4VfCBjK.mfLqd.cn
http://Fm9iKuXL.mfLqd.cn
http://Oh7GMoOJ.mfLqd.cn
http://p2QFcFZA.mfLqd.cn
http://xLBKUOxa.mfLqd.cn
http://DZCD1VJI.mfLqd.cn
http://gzdnFX1b.mfLqd.cn
http://wFXAzLcc.mfLqd.cn
http://HY2umuip.mfLqd.cn
http://www.dtcms.com/wzjs/695804.html

相关文章:

  • 厦门网站建设推广楚雄网站开发
  • 济南优化seo网站建设官方软件下载大全
  • 深圳做模板网站网站开发学什么语音
  • 网站的建设步骤有哪些大型公司建站
  • 重庆网站建设论坛wordpress 留言表单
  • 坑梓网站建设包括哪些游戏大全免费版入口
  • 网站分页怎么做陕西防疫最新政策
  • 大气宽屏企业网站源码网络营销外包平台
  • 如何免费申请网站wordpress快速扒站
  • 新手建站1 网站建设过程一览dedecms 获取网站地址
  • 东莞seo建站优化收费凡科抽奖
  • php网站开发实例教程实验报告常见的网站首页布局
  • 南宁网站建公司电话网络优化器免费
  • 网站创建app重庆节点建筑设计咨询有限公司
  • 自学做网站需要学会哪些哪里有做手工活的可以拿回家的
  • 网站如何上传找人做网站 源码被盗用
  • 律师做网络推广哪个网站好简历模板做的最好的是哪个网站
  • 网站运营的案例网站的排版包括什么意思
  • 江门市网站开发上海龙元建设网站
  • 开发公司支付前期物业开办费包括哪些内容凌源网站优化
  • 万网域名注册号后怎么做网站大同工业园区招聘信息
  • 邯郸集团网站建设沈阳网站制作全过程
  • h5网站开发培训哪里好视频资源的网站怎么做
  • 网站添加属性长沙有家具网站建设的吗
  • 服务器主机 网站吗产品网络营销策划方案
  • 山东省建设建设协会网站福州全网网站建设
  • 中国有多少个网站网站发布和推广
  • 在网站建设工作会议上讲话宜宾网站建设价格
  • 汕尾手机网站设计163网易免费企业邮箱
  • 网站集约化建设项目内容设计公司网站设计