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

南城网站建设公司报价北京360建筑网

南城网站建设公司报价,北京360建筑网,wordpress调用上传图片,h5个人网站模板下载Spring整合 1.1 Spring整合Mybatis思路分析1.1.1 环境准备步骤1:准备数据库表步骤2:创建项目导入jar包步骤3:根据表创建模型类步骤4:创建Dao接口步骤5:创建Service接口和实现类步骤6:添加jdbc.properties文件步骤7:添加Mybatis核心配置文件步骤8:编写应用程序步骤9:运行程序 1.… Spring整合 1.1 Spring整合Mybatis思路分析1.1.1 环境准备步骤1:准备数据库表步骤2:创建项目导入jar包步骤3:根据表创建模型类步骤4:创建Dao接口步骤5:创建Service接口和实现类步骤6:添加jdbc.properties文件步骤7:添加Mybatis核心配置文件步骤8:编写应用程序步骤9:运行程序 1.1.2 整合思路分析 1.2 Spring整合Mybatis步骤1:项目中导入整合需要的jar包步骤2:创建Spring的主配置类步骤3:创建数据源的配置类步骤4:主配置类中读properties并引入数据源配置类步骤5:创建Mybatis配置类并配置SqlSessionFactory步骤6:主配置类中引入Mybatis配置类步骤7:编写运行类步骤8:运行程序 1.3 Spring整合Junit1.3.1 环境准备1.3.2 整合Junit步骤步骤1:引入依赖步骤2:编写测试类 知识点1RunWith知识点2ContextConfiguration 1.1 Spring整合Mybatis思路分析 1.1.1 环境准备 步骤1:准备数据库表 Mybatis是来操作数据库表所以先创建一个数据库及表 create database spring_db character set utf8; use spring_db; create table tbl_account(id int primary key auto_increment,name varchar(35),money double );步骤2:创建项目导入jar包 项目的pom.xml添加相关依赖 dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.2.10.RELEASE/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.1.16/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.6/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.47/version/dependency /dependencies步骤3:根据表创建模型类 public class Account implements Serializable {private Integer id;private String name;private Double money;//setter...getter...toString...方法略 }步骤4:创建Dao接口 public interface AccountDao {Insert(insert into tbl_account(name,money)values(#{name},#{money}))void save(Account account);Delete(delete from tbl_account where id #{id} )void delete(Integer id);Update(update tbl_account set name #{name} , money #{money} where id #{id} )void update(Account account);Select(select * from tbl_account)ListAccount findAll();Select(select * from tbl_account where id #{id} )Account findById(Integer id); }步骤5:创建Service接口和实现类 public interface AccountService {void save(Account account);void delete(Integer id);void update(Account account);ListAccount findAll();Account findById(Integer id);}Service public class AccountServiceImpl implements AccountService {Autowiredprivate AccountDao accountDao;public void save(Account account) {accountDao.save(account);}public void update(Account account){accountDao.update(account);}public void delete(Integer id) {accountDao.delete(id);}public Account findById(Integer id) {return accountDao.findById(id);}public ListAccount findAll() {return accountDao.findAll();} }步骤6:添加jdbc.properties文件 resources目录下添加用于配置数据库连接四要素 jdbc.drivercom.mysql.jdbc.Driver jdbc.urljdbc:mysql://localhost:3306/spring_db?useSSLfalse jdbc.usernameroot jdbc.passwordrootuseSSL:关闭MySQL的SSL连接 步骤7:添加Mybatis核心配置文件 ?xml version1.0 encodingUTF-8? !DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configuration!--读取外部properties配置文件--properties resourcejdbc.properties/properties!--别名扫描的包路径--typeAliasespackage namecom.itheima.domain//typeAliases!--数据源--environments defaultmysqlenvironment idmysqltransactionManager typeJDBC/transactionManagerdataSource typePOOLEDproperty namedriver value${jdbc.driver}/propertyproperty nameurl value${jdbc.url}/propertyproperty nameusername value${jdbc.username}/propertyproperty namepassword value${jdbc.password}/property/dataSource/environment/environments!--映射文件扫描包路径--mapperspackage namecom.itheima.dao/package/mappers /configuration步骤8:编写应用程序 public class App {public static void main(String[] args) throws IOException {// 1. 创建SqlSessionFactoryBuilder对象SqlSessionFactoryBuilder sqlSessionFactoryBuilder new SqlSessionFactoryBuilder();// 2. 加载SqlMapConfig.xml配置文件InputStream inputStream Resources.getResourceAsStream(SqlMapConfig.xml.bak);// 3. 创建SqlSessionFactory对象SqlSessionFactory sqlSessionFactory sqlSessionFactoryBuilder.build(inputStream);// 4. 获取SqlSessionSqlSession sqlSession sqlSessionFactory.openSession();// 5. 执行SqlSession对象执行查询获取结果UserAccountDao accountDao sqlSession.getMapper(AccountDao.class);Account ac accountDao.findById(1);System.out.println(ac);// 6. 释放资源sqlSession.close();} }步骤9:运行程序 1.1.2 整合思路分析 Mybatis的基础环境我们已经准备好了接下来就得分析下在上述的内容中哪些对象可以交给Spring来管理? Mybatis程序核心对象分析 从图中可以获取到真正需要交给Spring管理的是SqlSessionFactory 整合Mybatis就是将Mybatis用到的内容交给Spring管理分析下配置文件 说明: 第一行读取外部properties配置文件Spring有提供具体的解决方案PropertySource,需要交给Spring第二行起别名包扫描为SqlSessionFactory服务的需要交给Spring第三行主要用于做连接池Spring之前我们已经整合了Druid连接池这块也需要交给Spring前面三行一起都是为了创建SqlSession对象用的那么用Spring管理SqlSession对象吗?回忆下SqlSession是由SqlSessionFactory创建出来的所以只需要将SqlSessionFactory交给Spring管理即可。第四行是Mapper接口和映射文件[如果使用注解就没有该映射文件]这个是在获取到SqlSession以后执行具体操作的时候用所以它和SqlSessionFactory创建的时机都不在同一个时间可能需要单独管理。 1.2 Spring整合Mybatis 前面我们已经分析了Spring与Mybatis的整合大体需要做两件事 第一件事是:Spring要管理MyBatis中的SqlSessionFactory 第二件事是:Spring要管理Mapper接口的扫描 具体该如何实现具体的步骤为: 步骤1:项目中导入整合需要的jar包 dependency!--Spring操作数据库需要该jar包--groupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.2.10.RELEASE/version /dependency dependency!--Spring与Mybatis整合的jar包这个jar包mybatis在前面是Mybatis提供的--groupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion1.3.0/version /dependency步骤2:创建Spring的主配置类 //配置类注解 Configuration //包扫描主要扫描的是项目中的AccountServiceImpl类 ComponentScan(com.itheima) public class SpringConfig { } 步骤3:创建数据源的配置类 在配置类中完成数据源的创建 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;Beanpublic DataSource dataSource(){DruidDataSource ds new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;} }步骤4:主配置类中读properties并引入数据源配置类 Configuration ComponentScan(com.itheima) PropertySource(classpath:jdbc.properties) Import(JdbcConfig.class) public class SpringConfig { } 步骤5:创建Mybatis配置类并配置SqlSessionFactory public class MybatisConfig {//定义beanSqlSessionFactoryBean用于产生SqlSessionFactory对象Beanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){SqlSessionFactoryBean ssfb new SqlSessionFactoryBean();//设置模型类的别名扫描ssfb.setTypeAliasesPackage(com.itheima.domain);//设置数据源ssfb.setDataSource(dataSource);return ssfb;}//定义bean返回MapperScannerConfigurer对象Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc new MapperScannerConfigurer();msc.setBasePackage(com.itheima.dao);return msc;} }说明: 使用SqlSessionFactoryBean封装SqlSessionFactory需要的环境信息 SqlSessionFactoryBean是前面我们讲解FactoryBean的一个子类在该类中将SqlSessionFactory的创建进行了封装简化对象的创建我们只需要将其需要的内容设置即可。方法中有一个参数为dataSource,当前Spring容器中已经创建了Druid数据源类型刚好是DataSource类型此时在初始化SqlSessionFactoryBean这个对象的时候发现需要使用DataSource对象而容器中刚好有这么一个对象就自动加载了DruidDataSource对象。 使用MapperScannerConfigurer加载Dao接口创建代理对象保存到IOC容器中 这个MapperScannerConfigurer对象也是MyBatis提供的专用于整合的jar包中的类用来处理原始配置文件中的mappers相关配置加载数据层的Mapper接口类MapperScannerConfigurer有一个核心属性basePackage就是用来设置所扫描的包路径 步骤6:主配置类中引入Mybatis配置类 Configuration ComponentScan(com.itheima) PropertySource(classpath:jdbc.properties) Import({JdbcConfig.class,MybatisConfig.class}) public class SpringConfig { }步骤7:编写运行类 在运行类中从IOC容器中获取Service对象调用方法获取结果 public class App2 {public static void main(String[] args) {ApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);AccountService accountService ctx.getBean(AccountService.class);Account ac accountService.findById(1);System.out.println(ac);} } 步骤8:运行程序 支持Spring与Mybatis的整合就已经完成了其中主要用到的两个类分别是: SqlSessionFactoryBeanMapperScannerConfigurer 1.3 Spring整合Junit 整合Junit与整合Druid和MyBatis差异比较大为什么呢Junit是一个搞单元测试用的工具它不是我们程序的主体也不会参加最终程序的运行从作用上来说就和之前的东西不一样它不是做功能的看做是一个辅助工具就可以了。 1.3.1 环境准备 直接使用Spring与Mybatis整合的环境即可。 1.3.2 整合Junit步骤 在上述环境的基础上我们来对Junit进行整合。 步骤1:引入依赖 pom.xml dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope /dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion5.2.10.RELEASE/version /dependency步骤2:编写测试类 在test\java下创建一个AccountServiceTest,这个名字任意 //设置类运行器 RunWith(SpringJUnit4ClassRunner.class) //设置Spring环境对应的配置类 ContextConfiguration(classes {SpringConfiguration.class}) //加载配置类 //ContextConfiguration(locations{classpath:applicationContext.xml})//加载配置文件 public class AccountServiceTest {//支持自动装配注入beanAutowiredprivate AccountService accountService;Testpublic void testFindById(){System.out.println(accountService.findById(1));}Testpublic void testFindAll(){System.out.println(accountService.findAll());} }注意: 单元测试如果测试的是注解配置类则使用ContextConfiguration(classes 配置类.class)单元测试如果测试的是配置文件则使用ContextConfiguration(locations{配置文件名,...})Junit运行后是基于Spring环境运行的所以Spring提供了一个专用的类运行器这个务必要设置这个类运行器就在Spring的测试专用包中提供的导入的坐标就是这个东西SpringJUnit4ClassRunner上面两个配置都是固定格式当需要测试哪个bean时使用自动装配加载对应的对象下面的工作就和以前做Junit单元测试完全一样了 知识点1RunWith 名称RunWith类型测试类注解位置测试类定义上方作用设置JUnit运行器属性value默认运行所使用的运行期 知识点2ContextConfiguration 名称ContextConfiguration类型测试类注解位置测试类定义上方作用设置JUnit加载的Spring核心配置属性classes核心配置类可以使用数组的格式设定加载多个配置类locations:配置文件可以使用数组的格式设定加载多个配置文件名称 后记 美好的一天到此结束下次继续努力欲知后续请看下回分解写作不易感谢大家的支持
文章转载自:
http://www.morning.crqbt.cn.gov.cn.crqbt.cn
http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn
http://www.morning.nd-test.com.gov.cn.nd-test.com
http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn
http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn
http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn
http://www.morning.skbbt.cn.gov.cn.skbbt.cn
http://www.morning.ylph.cn.gov.cn.ylph.cn
http://www.morning.srltq.cn.gov.cn.srltq.cn
http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn
http://www.morning.nba1on1.com.gov.cn.nba1on1.com
http://www.morning.bhwll.cn.gov.cn.bhwll.cn
http://www.morning.qgmbx.cn.gov.cn.qgmbx.cn
http://www.morning.kztpn.cn.gov.cn.kztpn.cn
http://www.morning.dqpnd.cn.gov.cn.dqpnd.cn
http://www.morning.smnxr.cn.gov.cn.smnxr.cn
http://www.morning.khlxd.cn.gov.cn.khlxd.cn
http://www.morning.pxlql.cn.gov.cn.pxlql.cn
http://www.morning.rkhhl.cn.gov.cn.rkhhl.cn
http://www.morning.nlgnk.cn.gov.cn.nlgnk.cn
http://www.morning.pwppk.cn.gov.cn.pwppk.cn
http://www.morning.qhmgq.cn.gov.cn.qhmgq.cn
http://www.morning.txlnd.cn.gov.cn.txlnd.cn
http://www.morning.glpxx.cn.gov.cn.glpxx.cn
http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn
http://www.morning.cwznh.cn.gov.cn.cwznh.cn
http://www.morning.knlgk.cn.gov.cn.knlgk.cn
http://www.morning.fmswb.cn.gov.cn.fmswb.cn
http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn
http://www.morning.drtgt.cn.gov.cn.drtgt.cn
http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn
http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com
http://www.morning.rjmb.cn.gov.cn.rjmb.cn
http://www.morning.bflws.cn.gov.cn.bflws.cn
http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn
http://www.morning.gkmwx.cn.gov.cn.gkmwx.cn
http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn
http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn
http://www.morning.yjprj.cn.gov.cn.yjprj.cn
http://www.morning.yggwn.cn.gov.cn.yggwn.cn
http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn
http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn
http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn
http://www.morning.frpfk.cn.gov.cn.frpfk.cn
http://www.morning.bpmdg.cn.gov.cn.bpmdg.cn
http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn
http://www.morning.nfks.cn.gov.cn.nfks.cn
http://www.morning.rbjp.cn.gov.cn.rbjp.cn
http://www.morning.zrjzc.cn.gov.cn.zrjzc.cn
http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn
http://www.morning.rfycj.cn.gov.cn.rfycj.cn
http://www.morning.ncrk.cn.gov.cn.ncrk.cn
http://www.morning.wbns.cn.gov.cn.wbns.cn
http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn
http://www.morning.gmjkn.cn.gov.cn.gmjkn.cn
http://www.morning.ftmly.cn.gov.cn.ftmly.cn
http://www.morning.wschl.cn.gov.cn.wschl.cn
http://www.morning.fksrg.cn.gov.cn.fksrg.cn
http://www.morning.qysnd.cn.gov.cn.qysnd.cn
http://www.morning.bztzm.cn.gov.cn.bztzm.cn
http://www.morning.tkyry.cn.gov.cn.tkyry.cn
http://www.morning.mdplm.cn.gov.cn.mdplm.cn
http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn
http://www.morning.glxdk.cn.gov.cn.glxdk.cn
http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn
http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn
http://www.morning.qcslh.cn.gov.cn.qcslh.cn
http://www.morning.nxrgl.cn.gov.cn.nxrgl.cn
http://www.morning.bswhr.cn.gov.cn.bswhr.cn
http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn
http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn
http://www.morning.jzmqk.cn.gov.cn.jzmqk.cn
http://www.morning.hrkth.cn.gov.cn.hrkth.cn
http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn
http://www.morning.djpzg.cn.gov.cn.djpzg.cn
http://www.morning.mplb.cn.gov.cn.mplb.cn
http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn
http://www.morning.mftzm.cn.gov.cn.mftzm.cn
http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn
http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn
http://www.tj-hxxt.cn/news/269447.html

相关文章:

  • 特产网站开发背景建站程序的选择
  • 一键生成广告图网站创建设计SEO优化象客
  • 网站建设公司一月赚多少大型淘宝客返利网站建设
  • 汕头网站制作开发宁晋网站开发搭建
  • 对外网站ipv6建设方案模板安徽全网优化
  • 淘宝接网站开发的活手机能制作游戏吗
  • 做网站的技术关键wordpress资讯cms主题
  • 青海省公路建设服务网站wordpress中文名图片不显示
  • 织梦网站0day漏洞哔哩哔哩网页版登陆
  • iis网站发布教程提供做网站企业
  • 第一章 网站建设基本概述关于茶叶网站模板
  • 网站建设公司福州有像考试佳园一样做资料的网站吗
  • 做外贸到那个网站石家庄免费网站建设
  • 辽宁营销型网站建设做网站什么内容
  • 做微商都去哪些网站留言久久建筑网官网登录入口
  • 无忧中英繁企业网站系统 破解Wordpress多重筛选插件
  • 网站搜索栏建立什么网站比较容易做
  • 个人怎样建网站seo是啥
  • 专业做公司网站六安网络上
  • 饰品销售网站功能建设wordpress 网页宽度
  • 网站建设前十名国内工业设计网站
  • 南京网站创建快懂百科登录入口
  • 网站建设考评表wordpress 七牛上传
  • 大什么的网站建设公司好百度竞价的优势和劣势
  • 网站排名优化昆明平台开发公司
  • 网站备案起名要求网站的格式分类
  • 网站建设策划基本流程图wordpress+3.2.1漏洞
  • 深圳建设工程交易中心网站济宁门户网站建设
  • 个人网站备案后内容可以改么官方网站如何做
  • 网站建设主要工作由哪些培训方案怎么做