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

局域网视频网站搭建在线制作网页系统

局域网视频网站搭建,在线制作网页系统,免费网站建设推广服务,建网站什么赚钱目录 一、纯MyBatis独立开发程序。 #xff08;1#xff09;数据库与数据表。 #xff08;2#xff09;实体类。 #xff08;3#xff09;dao层接口。#xff08;Mapper代理模式、无SQL映射文件——注解配置映射关系#xff09; #xff08;4#xff09;MyBatis核心配… 目录 一、纯MyBatis独立开发程序。 1数据库与数据表。 2实体类。 3dao层接口。Mapper代理模式、无SQL映射文件——注解配置映射关系 4MyBatis核心配置文件。 5应用测试程序。类App 二、Spring整合MyBatis的思路分析。Spring管理哪个bean 1MyBatis运行程序分析。 1初始化SqlSessionFactory。 2获取连接SqlSession。获取xxxMapper接口。 3调用数据层方法完成操作。 4关闭连接。 2MyBatis核心配置文件分析。 1初始属性、加载外部资源文件。 2初始化类型别名。 3配置数据源连接信息。 4初始化映射配置。 3分析小结。 三、Spring整合MyBatis。实操 1导入Spring整合Mybatis核心依赖坐标。 1spring-context。(基础核心) 2druid。(阿里数据源) 3mybatis。(基础核心) 4mysql-connector-java。(mysql核心jar) 5spring-jdbc。(spring操作数据库核心jar) 6mybatis-spring。(spring整合mybatis核心jar) 7lombok。快速开发 2service层。 1AccountService接口。 2AccountServiceImpl实现类。 3dao层。Mapper代理模式开发 4Jdbc配置类。配置数据源信息 5MyBatis配置类。取代MyBatis核心配置文件 1SqlSessionFactoryBean、MapperScannerConfigurer对象。 6Spring配置类。 7测试程序。类App02 一、纯MyBatis独立开发程序。 1数据库与数据表。 数据库hyl。数据表tb_account。 2实体类。 pom文件引入Lombok依赖。帮助快速开发无需手动提供getter、setter方法、构造器、toString()方法等... !--Lombok依赖坐标--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.30/versionscopeprovided/scope /dependency 在包domain下创建对应数据库字段的实体类用于封装。实体类上使用注解Data使Lombok生效。 package com.hyl.domain;import lombok.Data;Data public class Account {private Integer id;private String name;private Double money; }3dao层接口。Mapper代理模式、无SQL映射文件——注解配置映射关系 只有接口无实现类。使用Mapper代理模式这一套开发模式。无SQL映射文件。使用注解形式Select、Insert、Delete、Update配置其映射关系并完成SQL语句的书写。 package com.hyl.dao;import com.hyl.domain.Account; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update;import java.util.List;/*** 数据层的操作接口*/ public interface AccountDao {/*** 新增*/Insert(insert into tb_account (name,money) values (#{name},#{money}))void save(Account account);/*** 根据id删除*/Delete(delete from tb_account where id #{id})void delete(Integer id);/*** 更新* param account*/Update(update tb_account set name #{name} , money #{money} where id #{id})void update(Account account);/*** 查询所有* return*/Select(select * from tb_account )ListAccount selectAll();/*** 根据id查询* param id* return*/Select(select * from tb_account where id #{id})Account selectById(Integer id); }4MyBatis核心配置文件。 pom文件引入mybatis核心依赖与Java连接数据库核心依赖。 !-- https://mvnrepository.com/artifact/org.mybatis/mybatis -- !--mybatis依赖-- dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.6/version /dependency!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -- !--mysql驱动-- dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.28/version/dependency properties加载外部properties配置文件。 jdbc.drivercom.mysql.cj.jdbc.Driver jdbc.urljdbc:mysql://localhost:3306/hyl jdbc.usernameroot jdbc.passwordroot123 加载JDBC连接MySQL的数据源连接信息。mappers中的package注册指定包下的所有mapper接口。 ?xml version1.0 encodingUTF-8 ? !DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configurationproperties resourcedb.properties/propertiessettingssetting namelogImpl valueSTDOUT_LOGGING//settingstypeAliasespackage namecom.hyl.domain//typeAliasesenvironments defaultdevelopmentenvironment iddevelopment!--配置JDBC事务管理--transactionManager typeJDBC/transactionManagerdataSource typePOOLEDproperty namedriver value${jdbc.driver}/property nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}//dataSource/environment/environmentsmapperspackage namecom.hyl.dao//mappers /configuration 5应用测试程序。类App 获得SqlSessionFactory对象。获得SqlSession对象。通过SqlSession对象调用getMapper(执行业务的接口.class)获取真正执行业务操作的xxxMapper接口。通过调用对应的方法执行查询操作即可。最后释放SqlSession对象资源。 package com.hyl;import com.hyl.dao.AccountDao; import com.hyl.domain.Account; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException; import java.io.InputStream;public class App {public static void main(String[] args) throws IOException {//1.创建sqlSessionFactoryBuilder对象SqlSessionFactoryBuilder sqlSessionFactoryBuilder new SqlSessionFactoryBuilder();//加载mybatis主配置文件InputStream resourceAsStream Resources.getResourceAsStream(mybatisConfig.xml);//3.创建SqlSessionFactory对象SqlSessionFactory sqlSessionFactory sqlSessionFactoryBuilder.build(resourceAsStream);//4.创建SqlSession对象SqlSession sqlSession sqlSessionFactory.openSession();//5.执行SqlSession对象的getMapper方法获取执行业务的接口MapperAccountDao accountDao sqlSession.getMapper(AccountDao.class);Account account accountDao.selectById(1);System.out.println(查询结果account);//6.释放SqlSession资源sqlSession.close();} }测试类App程序运行结果如下。 二、Spring整合MyBatis的思路分析。Spring管理哪个bean 1MyBatis运行程序分析。 从上面的纯MyBatis开发项目的运行程序App分析。并将其拆分为几个重要的模块。 因为Spring是用来管理bean的那么需要确定有哪些bean是需要交给Spring管理的 1初始化SqlSessionFactory。 核心对象SqlSessionFactory。需要交给spring管理 2获取连接SqlSession。获取xxxMapper接口。 无核心需要被spring管理的对象。SqlSession对象是由SqlSessionFactory初始化那个阶段就已经造好。 3调用数据层方法完成操作。 无核心需要被spring管理的对象。虽然accountDao是执行业务的对象但依旧不是根源的对象。并且随着业务的更换造出的对象也会更新。 4关闭连接。 无核心需要被spring管理的对象。 2MyBatis核心配置文件分析。 MyBatis核心配置文件所有的配置都是围绕着SqlSessionFactory对象进行的。 1初始属性、加载外部资源文件。 读取外部文件的值。不是核心关键所在。 2初始化类型别名。 操作数据库得到的数据结果进行封装到对应的实体类中。次要核心。 3配置数据源连接信息。 配置数据源对象DataSource。核心所在。造出SqlSession对象是操作对应的数据库的。而这些配置都是为核心对象SqlSessionFactory服务的。 4初始化映射配置。 这部分主要关于业务的操作。初始化SqlSessionFactory对象后再根据不同的配置、不同的接口获得不同的xxxMapper再去操作不同的库与表。所以也不是核心关键所在。 3分析小结。 最终经过分析MyBatis核心的对象SqlSessionFactory是需要交给Spring进行管理的。 三、Spring整合MyBatis。实操 注数据库、数据表、实体类的代码与纯MyBatis开发程序一致。 1导入Spring整合Mybatis核心依赖坐标。 1spring-context。(基础核心) dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.18/version/dependency 2druid。(阿里数据源) !--阿里数据库连接池druid--dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.2.8/version/dependency 3mybatis。(基础核心) !-- https://mvnrepository.com/artifact/org.mybatis/mybatis --!--mybatis依赖--dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.6/version/dependency 4mysql-connector-java。(mysql核心jar) !-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --!--mysql驱动--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.28/version/dependency 5spring-jdbc。(spring操作数据库核心jar) !-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --dependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.3.18/version/dependency 6mybatis-spring。(spring整合mybatis核心jar) 注意这个jar与mybatis的基础依赖的版本是有联系的。随着版本的更替都会更替。 !-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --dependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion1.3.1/version/dependency 7lombok。快速开发 !--Lombok依赖坐标--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.30/versionscopeprovided/scope/dependency 2service层。 1AccountService接口。 package com.hyl.service;import com.hyl.domain.Account;import java.util.List;public interface AccountService {/*** 新增* param account*/void save(Account account);/*** 更新* param account*/void update(Account account);/*** 删除* param id*/void delete(Integer id);/*** 根据id查询* param id* return*/Account selectById(Integer id);/*** 查询所有* return*/ListAccount selectAll(); }2AccountServiceImpl实现类。 使用注解Service将该实现类交给spring容器管理。使用注解Autowired完成dao层AccountDao的自动装配。 package com.hyl.service.impl;import com.hyl.dao.AccountDao; import com.hyl.domain.Account; import com.hyl.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List;Service public class AccountServiceImpl implements AccountService {Autowiredprivate AccountDao accountDao;Overridepublic void save(Account account) {accountDao.save(account);}Overridepublic void update(Account account) {accountDao.update(account);}Overridepublic void delete(Integer id) {accountDao.delete(id);}Overridepublic Account selectById(Integer id) {return accountDao.selectById(id);}Overridepublic ListAccount selectAll() {return accountDao.selectAll();} }3dao层。Mapper代理模式开发 AccountDao接口。无SQL映射文件。使用注解Selcet、Insert、Delete、Update配置映射关系与SQL语句 package com.hyl.dao;import com.hyl.domain.Account; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update;import java.util.List;/*** 数据层的操作接口*/ public interface AccountDao {/*** 新增*/Insert(insert into tb_account (name,money) values (#{name},#{money}))void save(Account account);/*** 根据id删除*/Delete(delete from tb_account where id #{id})void delete(Integer id);/*** 更新* param account*/Update(update tb_account set name #{name} , money #{money} where id #{id})void update(Account account);/*** 查询所有* return*/Select(select * from tb_account )ListAccount selectAll();/*** 根据id查询* param id* return*/Select(select * from tb_account where id #{id})Account selectById(Integer id); }4Jdbc配置类。配置数据源信息 使用注解Bean标明返回数据源对象的方法。这样Spring会自动将DataSource对象交给Spring容器管理。其它的组件bean都可以使用。 package com.hyl.config;import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean;import javax.sql.DataSource;public class JdbcConfig {Value(${jdbc.driver})private String driver;Value(${jdbc.url})private String url;Value(${jdbc.username})private String userName;Value(${jdbc.password})private String password;/*** 1、定义方法返回需要管理的bean这里使用阿里提供的第三方数据源druid* 2、使用注解Bean 将方法的返回值声明为一个Spring管理的Bean。* 这意味着Spring会调用这个方法并将方法的返回值bean存储到Spring容器中供其他组件使用。*/Beanpublic DataSource dataSource(){DruidDataSource druidDataSource new DruidDataSource();druidDataSource.setDriverClassName(driver);druidDataSource.setUrl(url);druidDataSource.setUsername(userName);druidDataSource.setPassword(password);return druidDataSource;} }5MyBatis配置类。取代MyBatis核心配置文件 使用注解Bean标明返回SqlSessionFactory对象的方法。这样Spring会自动将SqlSessionFactory对象交给Spring容器管理。其它的组件bean都可以使用。 因为根据上方分析SqlSessionFactory对象的初始化都与MyBatis核心配置文件有关所以在返回SqlSessionFactory的方法里面要设置很多东西 1SqlSessionFactoryBean、MapperScannerConfigurer对象。 为了简化开发spring整合mybatis中提供了类SqlSessionFactoryBean制造bean。 最终的Mybatis配置类的代码如下。 package com.hyl.config;import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.Bean;import javax.sql.DataSource;public class MyBatisConfig {Beanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){SqlSessionFactoryBean ssfb new SqlSessionFactoryBean();//代替Mybatis核心配置文件标签typeAliasesssfb.setTypeAliasesPackage(com.hyl.domain);//因为Jdbc配置类的方法使用了Bean注解生产DataSource对象的方法。// 则可以使用形参注入DataSource。再通过方法设置使DataSourcessfb.setDataSource(dataSource);//jdbc事务管理默认有spring-jdbc依赖处理return ssfb;}//单独方法代替Mybatis核心配置文件标签Mappers//使用spring整合mybatis提供的类MapperScannerConfigurer完成映射文件的扫描Beanpublic MapperScannerConfigurer mapperScannerConfigurer() {MapperScannerConfigurer mapperScannerConfigurer new MapperScannerConfigurer();//设置映射在哪些包下mapperScannerConfigurer.setBasePackage(com.hyl.dao);return mapperScannerConfigurer;} }加后缀.bak——注释原来的MyBatis核心配置文件。方便测试。 6Spring配置类。 为了快速的开发——所以选择纯注解的开发模式。而放弃使用XML配置文件开发模式。使用注解Configuration标明这是spring的一个配置类。平替spring配置文件使用注解ComponentScan扫描指定包下类的注解。使用注解PropertySource加载外部properties配置文件。使用注解Import引入其它的配置类JdbcConfig、MyBatisConfig配置类。 package com.hyl.config;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource;Configuration ComponentScan(com.hyl) PropertySource(classpath:jdbc.properties) Import({JdbcConfig.class,MyBatisConfig.class}) public class SpringConfig {}7测试程序。类App02 通过类AnnotationConfigApplicationContext加载spring配置类。 spring配置类再通过注解完成其它配置类的扫描、包的注解扫描。 配和注解Service使用用容器对象调用getBean()方法获取AccountServiceImpl对象。 最后调用操作数据库的方法。这里以测试根据id查询演示。 package com.hyl;import com.hyl.config.SpringConfig; import com.hyl.domain.Account; import com.hyl.service.AccountService; import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class App02 {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(SpringConfig.class);AccountService accountService applicationContext.getBean(AccountService.class);Account account accountService.selectById(2);System.out.println(account);applicationContext.close();} }测试类App02程序运行结果如下。 到这里就是就实现了Spring整合MyBatis的全部操作了也是完成快速开发的重要一步。
http://www.w-s-a.com/news/17896/

相关文章:

  • 网站开发招聘信息匿名ip访问网站受限
  • 网站转app工具网站规划建设与管理维护大作业
  • flash是怎么做网站的.net购物网站开发
  • 烟台网站建设求职简历品质商城网站建设
  • 做百度外链哪些网站权重高点做网站具备的条件
  • 怎么样用ppt做网站红番茄 网站点评
  • 建设银行河北分行招聘网站哪里能找到网站
  • 兰州营销型网站网站建设收费标准
  • 网站首页动图怎么做自己做网站很难
  • 自建网站如何盈利推广引流最快的方法
  • 网页设计网站结构图怎么弄网站用户 分析
  • 企业手机网站建设策划天津网页设计工作
  • 苏州vr全景网站建设公司怎么讲解网页的制作技术
  • 徐州智能建站怎么做苏州建设网站首页
  • 网站支付功能报价wordpress主页透明
  • asia域名的网站宁波模板建站源码
  • 官网网站怎么做个人网站盈利
  • 青龙桥网站建设网站同时做竞价和优化可以
  • 沭阳建设网站婴儿辅食中企动力提供网站建设
  • 常州做网站的公司济宁网站建设seo
  • 用wordpress做企业网站视频教程韶关建设网站
  • 怎么做一个免费的网站云南网站设计选哪家
  • dw做六个页面的网站做网站运营有前途吗
  • 中级网站开发工程师 试题战地之王网站做任务
  • 广东东莞保安公司湖南 seo
  • 无锡网站策划公司如何零基础学编程
  • 金融网站如何做设计网站开发流程 文档
  • 用jsp做网站国内知名设计工作室
  • 一键搭建网站北京公司网站设计
  • 山东省城乡建设部网站网站营销单页怎么做