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

沈阳网站提升排名中国移动crm系统

沈阳网站提升排名,中国移动crm系统,新手建站,网站建设方案书微商城一、学习目标 spring整合MyBatis的原理主要涉及到将MyBatis的Mapper映射文件交由Spring容器管理#xff0c;并将其注入到MyBatis的SqlSessionFactory中#xff0c;从而实现两者的整合。 二、整合mybatis 1.写一个mybatis测试案例 项目结构#xff1a; 1.数据库 CREATE DA…一、学习目标 spring整合MyBatis的原理主要涉及到将MyBatis的Mapper映射文件交由Spring容器管理并将其注入到MyBatis的SqlSessionFactory中从而实现两者的整合。 二、整合mybatis 1.写一个mybatis测试案例 项目结构 1.数据库 CREATE DATABASE mybatis; USE mybatis;create table Users(id int not null auto_increment primary key,name varchar(10) not null,age int not null );insert into Users(id,name,age) values(null,张三,20),(null,李四,18);2.实体类 public class User {private int id; //idprivate String name; //姓名private int age; //密码public int getId() {return id;}public void setId(int id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public User(int id, String name, int age) {this.id id;this.name name;this.age age;}Overridepublic String toString() {return User{ id id , name name \ , age age };}public User() {} }2.编写接口 public interface UserMapper {public ListUser selectUser(); } 3.mybatisConfig配置文件 ?xml version1.0 encodingUTF-8 ? !DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configurationtypeAliasespackage namecom.lzh.pojo//typeAliasesenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver valuecom.mysql.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/mybatis?useSSLtrueamp;useUnicodetrueamp;characterEncodingutf8amp;useSSLfalse/property nameusername valueroot/property namepassword valueadmin123//dataSource/environment/environmentsmapperspackage namecom.lzh.dao//mappers /configuration 4.UserrMapper文件 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.lzh.dao.UserMapperselect idselectUser resultTypeUserselect * from users/select /mapper 5.pom.xml ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.lzh/groupIdartifactIdspring-07/artifactIdversion1.0-SNAPSHOT/versiondependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.2/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.47/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.1.10.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.1.10.RELEASE/version/dependency!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.4/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion2.0.2/version/dependency/dependenciesbuildresourcesresourcedirectorysrc/main/java/directoryincludesinclude**/*.properties/includeinclude**/*.xml/include/includesfilteringtrue/filtering/resource/resources/buildpropertiesmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/properties/project 6.测试 Testpublic void selectUser() throws IOException {String resource MybatisConfig.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession sqlSessionFactory.openSession();UserMapper mapper sqlSession.getMapper(UserMapper.class);ListUser userList mapper.selectUser();for (User user: userList){System.out.println(user);}sqlSession.close();} 2.整合mybatis方式一 1.配置数据源 !--配置数据源-定义一个数据源beanid为dataSource用于管理数据库连接。class属性指定了数据源的实现类为org.springframework.jdbc.datasource.DriverManagerDataSource--bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSourceproperty namedriverClassName valuecom.mysql.jdbc.Driver/!--设置数据库连接的URL。- useSSLtrueamp;useSSLfalse这里似乎有一个错误通常只需要一个useSSL参数并且应该保持一致。对于本地开发通常不需要SSL所以应设置为false。- useUnicodetrue指定使用Unicode字符集。- characterEncodingutf8指定字符编码为UTF-8。注意从MySQL 5.5.3版本开始建议使用utf8mb4代替utf8以支持更广泛的Unicode字符包括emoji表情等。--property nameurl valuejdbc:mysql://localhost:3306/mybatis?useSSLfalseamp;useUnicodetrueamp;characterEncodingutf8/property nameusername valueroot/property namepassword valueadmin123//bean 2.配置SqlSessionFactory关联MyBatis注册,sqlSessionTemplate关联sqlSessionFactory !-- 配置SqlSessionFactory --bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBean!-- 注入数据源这里的ref指向之前定义的数据源bean --property namedataSource refdataSource/!-- 关联Mybatis的全局配置文件MybatisConfig.xml中包含了MyBatis的设置如别名、类型处理器、插件等 --property nameconfigLocation valueclasspath:MybatisConfig.xml/!-- 指定Mapper XML文件的位置MyBatis会根据这些XML文件来创建Mapper接口的实现 --!-- 注意这里只指定了一个Mapper XML文件如果有多个可以使用逗号分隔或者使用通配符 --property namemapperLocations valueclasspath:com/lzh/dao/UserMapper.xml//bean!-- 注册sqlSessionTemplate关联sqlSessionFactory --bean idsqlSession classorg.mybatis.spring.SqlSessionTemplate!-- 通过构造器注入SqlSessionFactory使得SqlSessionTemplate能够管理SqlSession --!-- index0指定了构造器参数的索引这里假设SqlSessionTemplate的构造器第一个参数就是SqlSessionFactory --constructor-arg index0 refsqlSessionFactory//bean 3.增加接口实现类 public class UserDaoImpl implements UserMapper {// 注入SqlSessionTemplateSpring容器会负责注入private SqlSessionTemplate sqlSession;// 通过构造器注入SqlSessionTemplate推荐的方式因为它支持Spring的依赖注入最佳实践public void setSqlSession(SqlSessionTemplate sqlSession) {this.sqlSession sqlSession;}public ListUser selectUser() {UserMapper mapper sqlSession.getMapper(UserMapper.class);return mapper.selectUser();} } 4.注册bean bean iduserDao classcom.lzh.dao.UserDaoImplproperty namesqlSession refsqlSession//bean 5.修改mybatis配置文件 !--mapperspackage namecom.lzh.dao//mappers-- 6.测试 Testpublic void test2(){ApplicationContext context new ClassPathXmlApplicationContext(beans.xml);UserMapper mapper (UserMapper) context.getBean(userDao);ListUser user mapper.selectUser();System.out.println(user);} 2.整合mybatis方式二 1.修改接口实现类 public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper {public ListUser selectUser() {UserMapper mapper getSqlSession().getMapper(UserMapper.class);return mapper.selectUser();} } 2.修改bean配置 !-- 注册sqlSessionTemplate关联sqlSessionFactory --!--bean idsqlSession classorg.mybatis.spring.SqlSessionTemplate 通过构造器注入SqlSessionFactory使得SqlSessionTemplate能够管理SqlSession --!-- index0指定了构造器参数的索引这里假设SqlSessionTemplate的构造器第一个参数就是SqlSessionFactory --!--constructor-arg index0 refsqlSessionFactory//bean--!--bean iduserDao classcom.lzh.dao.UserDaoImplproperty namesqlSession refsqlSession//bean--bean iduserDao classcom.lzh.dao.UserDaoImplproperty namesqlSessionFactory refsqlSessionFactory //bean
http://www.laogonggong.com/news/135963.html

相关文章:

  • 浙江省长兴县建设局网站菠菜网站怎么做
  • 北京网站推广seo优化网站怎么做模板切换
  • 学校网站建设运行情况简介门户网站是指提供什么的网站
  • 高性能网站开发 书籍wordpress aws
  • 精细化学品网站建设建设一个网站要多少钱
  • 龙华做网站 熊掌号企业登记代理公司
  • 在网上找做设计是什么网站网站建设幽默
  • 装修公司网站制作怎么做旅游网站框架
  • 郑州app开发公司排名东莞市seo网络推广平台
  • 如何使用凡科建设网站龙泉网站建设
  • 重庆交通建设集团网站conoha wordpress
  • 怎么做网站教程 建站视频子域名大全
  • 高端网站建设收费为何比较贵武昌做网站报价
  • 苏州网站开发公司电话网站开发选择题
  • 张店制作网站企业能建站吗
  • 找柳市做网站青岛有哪些大型的互联网公司
  • 营销网站的案例分析兰州网络广告设计方案
  • 苏州商城网站建设电话网站开发有哪些公司
  • 西安百度网站建设网址查询域名解析
  • 个人音乐网站源码搭建郑州市建筑材料信息价
  • 永兴网站建设广州网站建设需要多少费用
  • 怎么利用源码做网站网站群建设的目的
  • 高明网站制作WordPress设置API
  • html5做静态网站中韩双语网站制作价格
  • 网站建设成本核算模板大连开发区网络公司
  • 定制网站建设的流程图wordpress 首页显示分类文章列表
  • 大连制作企业网站邢台网站建设优化
  • 合肥制作网站的公司wordpress搜索慢怎么解决
  • 顺德网站建设公司价格人工智能培训心得体会
  • 广州公司建设网站wordpress采集淘宝客