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

cms 企业网站管理系统做模型挣钱的网站

cms 企业网站管理系统,做模型挣钱的网站,社交网站开发公司,梦创义网站建设目录#xff1a; Spring JDBC1.Spring JDBC的核心类 ( JdbcTemplate类 )2.Srping JDBC 的配置3.JdbcTemplate类的“常用方法”execute( )#xff1a;直接执行“sql语句”#xff0c;没有返回值update( ) #xff1a;“增删改”#xff0c;返回 “影响的行数”query( ) : “… 目录 Spring JDBC1.Spring JDBC的核心类 ( JdbcTemplate类 )2.Srping JDBC 的配置3.JdbcTemplate类的“常用方法”execute( )直接执行“sql语句”没有返回值update( ) “增删改”返回 “影响的行数”query( ) : “查询”返回 “T类型 / List类型” 的结果 作者简介 一只大皮卡丘计算机专业学生正在努力学习、努力敲代码中! 让我们一起继续努力学习 该文章参考学习教材为 《Java EE企业级应用开发教程 (Spring Spring MVC MyBatis)》 黑马程序员 / 编著 文章以课本知识点 代码为主线结合自己看书学习过程中的理解和感悟 最终成就了该文章 文章用于本人学习使用 同时希望能帮助大家。 欢迎大家点赞 收藏⭐ 关注哦 侵权教材方可联系我进行删除如果雷同纯属巧合 Spring JDBC Spring的 JDBC模块 负责数据库资源管理和错误处理大大简化了开发人员对数据库的操作使得开发人员可以从烦琐的数据库操作中解脱出来从而将更多的精力投入到编写业务逻辑中。 ​ 1.Spring JDBC的核心类 ( JdbcTemplate类 ) 针对数据库的操作Spring 框架 (Spring JDBC )提供了 JdbcTemplate类该类是Spring框架数据抽象层的基础其他更高层次的抽象类却是构建于JdbcTemplate类之上。 JdbcTemplate 类是Spring JDBC的核心类。 JdbcTemplate的继承 / 实现 关系为 : 抽象类赋予JdbcTemplate属性接口赋予JdbcTemplate操作的方法具体内容如下 一、 JdbcTemplate 继承 抽象类 JdbcAccessor。JdbcAccessor是 JdbcTemplate的直接父类该类 (JdbcAccessor) 为子类 (JdbcTemplate) 提供了一些访问数据库时使用的公共属性。 ① DataSource : 其主要功能是获取数据库连接具体实现时还可以引入对数据库连接的缓 冲池和分布式事务的支持它可以作为访问数据库资源的标准接口。 ② SQLExceptionTranslator org.springframework.jdbc support.SQLExceptionTranslator接口负责对 SQLException 进行转译工作。通过必要的设置或者 获取SQLExceptionTranslator中的方法可以使 JdbcTemplate 在需要处理SQLException时委托SQLExceptionTranslator的实现类来完成相关的转译工作。 二、 JdbcTemplate 实现了 JdbcOperations 接口。JdbcOperations接口定义了在JdbcTemplate类中可以使用的操作集合包括 添加、修改、查询和删除 等操作。 2.Srping JDBC 的配置 Spring JDBC模块主要由 4个包组成分别是core (核心包)、dataSource (数据源包)、object (对象包)和support (支持包)。 要 想实现 Spring JDBC功能要进行Spring JDBC配置 : ① 配置 core (核心包) 中的 JdbcTemplate 类。 ② 配置 dataSource (数据源包) 中的 DriverManagerDataSource类 (在 applicationContext.xml 中进行配置) 包名说明core包 (要配置JdbcTemplate 类)① 包含了JDBC的核心功能包括 JdbcTemplate 类、SimpleJdbcInsert类、SimpleJdbcCall类 以及 NamedParameterJdbcTemplate类。② 定义JdbcTemplate时要将已经配置好的dataSource注入到JdbcTemplate中。dataSource包 (要配置dataSource数据源 : DriverManagerDataSource类 )① 访问数据源的实用工具类它有多种数据源的实现可以在Java EE容器外部测试JDBC代码。② 在配置文件中配置dataSource时其的类为 org.springframework.jdbc.datasource.DriverManagerDataSource③ 配置 dataSource 的4个属性分别为: 数据库驱动、url、用户名、 密码ps 配置dataSource的例子在下面。object包以面向对象的方式访问数据库它允许执行查询并将返回结果作为业务对象可以在数据表的列 和 业务对象的属性之间映射查询结果。support包包含了core和object包的支持类例如提供异常转换功能的SQLException类。 dataSource的4个属性 ( 在以下的dataSource的4个属性 在applicationContext.xml中完成配置 ) 属性名含义driverClassName所使用的驱动名词对应驱动JAR包中工的Driver类。如 property name“driverClassName” value“com.mysql.jdbc.Driver”/url数据源所在地址。如 property name“url” value“jdbc:mysql://localhost:3306/spring”/username访问数据库的用户名。如: property name“username” value“root”/password访问数据库的密码。如 : property name“password” value“root”/ Srping JDBC的配置的 “配置模板” / 例子 Spring JDBC要添 JAR包 : (配置JAR包) ① Spring的核心JAR包 ②Spring JDBC开发所需JAR mysql数据库的驱动JAR包 (mysql-connector-java.jar) 、Srpring的JDBC的JAR包 (spring-jdbc.jar) 、Spring事务处理的JAR包(spring-tx.jar)。 获取spring框架基本核心jar包 获取Spring JDBC 开发需要的jar包 jar包 / maven( 依赖 ) 下载( 可自行按需下载JAR ) ps 如有报错或版本问题可看情况判断是否需要更换JAR版本。 applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!-- ①该配置文件中可添加、配置管理Bean ②可配置Spring AOP (AspectJ等)的配置信息 ③当然也可以配置Spring JDBC等的配置信息 --!-- 1.配置数据源 (与Spring JDBC中的dataSource模块有关) -- bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSource!-- 数据库驱动 --property namedriverClassName valuecom.mysql.jdbc.Driver/!-- url --property nameurl valuejdbc:mysql://localhost:3306/spring/!-- 用户名 --property nameusername valueroot/!-- 密码 --property namepassword valueroot/ /bean!-- 2.配置JDBC模板 / 配置JdbcTemplate (与Spring JDBC中的core模块有关) -- bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplate!-- 默认必须使用数据源 --property namedataSource refdataSource/ /bean/beans在上述applicationContext.xml中定义了 3个Bean分别是 dataSoure 、jdbcTemplate 和 需要注入类的Bean。 其中 dataSource 对应 的org.springframework.jdbc.datasource. DriverManagerDataSource 类用于对数据源进行配置。 jdbcTemplate 对应的org.springframework.jdbc.core. JdbcTemplate 类用于定义了JdbcTemplate的相关配置。 dataSource的4个属性需要根据数据库类型或者机器配置的不同设置相应的属性值。例如果数据库类型不同需要更改驱动名称如果数据库不在本地则需要将地址中的 localhost 替换成相应的主机IP如果修改过MySQL数据库的端口号(默认为3306)则需要加上修改后的端口号如果没修改则端口号可以省略。 3.JdbcTemplate类的“常用方法” 在 JdbcTemplate类中提供了大量的更新和查询数据库的方法我们就是使用这些方法来操作数据库的。 execute( )直接执行“sql语句”没有返回值 execute (String sql) 方法 : 执行指定的 SQL 语句。该方法是没有返回值的。 例子如 第一步、建好项目、导入所需依赖、打开doc窗口指定确定存在的数据库 (如: use spring) ​ 此时spring这个 “数据库” 中并没有 “数据表”。 第二步、配置applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--该配置文件中可以配置Spring JDBC等的配置信息--!-- 1.配置数据源 (与Spring JDBC中的dataSource模块有关) --bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSource!-- 数据库驱动 --property namedriverClassName valuecom.mysql.jdbc.Driver/!-- url --property nameurl valuejdbc:mysql://localhost:3306/spring/!-- 用户名 --property nameusername valueroot/!-- 密码 --property namepassword valueroot//bean!-- 2.配置JDBC模板 / 配置JdbcTemplate (与Spring JDBC中的core模块有关) --bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplate!-- 默认必须使用数据源 --property namedataSource refdataSource//bean/beans第三步、创建 JdbcTemplateTest测试类测试 JdbcTemplate类中的 execute( )方法的使用情况 : package com.myh.jdbc;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate;public class JdbcTemplateTest { //在该测试类中使用 execute()方法建数据库中的表public static void main(String[] args) {//加载配置文件ApplicationContext applicationContext new ClassPathXmlApplicationContext(com/myh/jdbc/applicationContext.xml);//从IOC容器中获得 JdbcTemplate 实例JdbcTemplate jdbcTemplate (JdbcTemplate) applicationContext.getBean(jdbcTemplate);String sql create table student( id int primary key auto_increment, username varchar(50), hobby varchar(50));/*execute()方法作用 : 执行sql语句*/jdbcTemplate.execute(sql);System.out.println(创建student表成功);} }运行效果图 doc窗口输入 “show tables”命令判断execute( )方法是否成功执行 控制台输入 如下 : 由两个效果图可知程序 使用execute( String sql )方法执行的sql语句成功创建student表。 update( ) “增删改”返回 “影响的行数” update()方法 : 可以完成 插入、更新 和 删除数据 的操作有返回值 : 返回影响的行数。 在JdbcTemplate类中提供了一系列的 update( )方法其常用方法如下所示 方法说明int update( String sql )该方法是最简单的update 方法重载形式它直接执行传入的SQL语句并 返回受影响的行数。int update( PreparedStatementCreator psc )该方法执行从PreparedStatementCreator返回的语句然后返回受影响的行数。int update( String sql,PreparedStatementSetter )该方法通过PreparedStatementSetter设置SQL语句中参数并返回受影响的行数。int update( String sql,Object…args ) ★★★★★ ( 常用 )该方法 使用Object…设置SQL语句中的参数 要求参数不能为NULL并 返回受影响的行数。将设置的参数填充到占位符?中 update( ) 方法例子 进行“增删改”操作。 Student.java package com.myh.jdbc;public class Student {private Integer id; //学生idprivate String username; //用户名private String hobby;/*getter/setter方法*/public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby hobby;}//重写toString()方法Overridepublic String toString() {return Student{ id id , username username \ , hobby hobby \ };} }StudentDao.java 接口 : package com.myh.jdbc;public interface StudentDao { //在该接口中定义“添加”、“更新”、“删除”的Student的方法//添加public int addStudent(Student student);//更新public int updateStudent(Student student);//删除public int deleteStudent(int id); }StudentDaoImpl.java 实现类 package com.myh.jdbc;import org.springframework.jdbc.core.JdbcTemplate; public class StudentDaoImpl implements StudentDao{ //该类为StudentDao接口的实现类//声明JdbcTemplate属性private JdbcTemplate jdbcTemplate;//为JdbcTemplate属性 添加setter方法public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate jdbcTemplate;}/**添加*/Overridepublic int addStudent(Student student) {/*sql语句,此处的占位符?通过下面的Object参数进行填充*/String sql insert into student(username,hobby) values(?,?);//定义数组来存储sql语句中的参数 (将Student类中存储的数组放进数组中)Object[] obj new Object[]{student.getUsername(),student.getHobby()};//通过 update(String sql)执行操作,返回值为: sql语句影响的行数int num this.jdbcTemplate.update(sql,obj); //返回sql语句影响的行数return num;}/*** 更新*/Overridepublic int updateStudent(Student student) {String sql update student set username ?,hobby ? where id ?;//定义要用在update()方法中的参数Object[] params new Object[]{student.getUsername(), student.getHobby(), student.getId()};int num this.jdbcTemplate.update(sql, params);return num;}/*** 删除*/Overridepublic int deleteStudent(int id) {String sql delete from student where id ?;//执行删除操作,返回影响的行数int num this.jdbcTemplate.update(sql, id);return num;} }applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--该配置文件中可以配置Spring JDBC等的配置信息--!-- 1.配置数据源 (与Spring JDBC中的dataSource模块有关) --bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSource!-- 数据库驱动 --property namedriverClassName valuecom.mysql.jdbc.Driver/!-- url --property nameurl valuejdbc:mysql://localhost:3306/spring/!-- 用户名 --property nameusername valueroot/!-- 密码 --property namepassword valueroot//bean!-- 2.配置JDBC模板 / 配置JdbcTemplate (与Spring JDBC中的core模块有关) --bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplate!-- 默认必须使用数据源 --property namedataSource refdataSource//bean!--定义一个id 为studentDao 的Bean,该Bean用于将jdbcTemplate类注入到studentDao这个实例中因为实例中要用到该对象--bean idstudentDao classcom.myh.jdbc.StudentDaoImplproperty namejdbcTemplate refjdbcTemplate//bean/beansJdbcTemplateTest.java (测试类) package com.myh.jdbc;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate;public class JdbcTemplateTest { //在该测试类中使用 execute()方法建数据库中的表/*** 进行Junit测试*/Testpublic void JunitTest() {//加载配置文件ApplicationContext applicationContext new ClassPathXmlApplicationContext(com/myh/jdbc/applicationContext.xml);//从IOC容器中获得 JdbcTemplate 实例JdbcTemplate jdbcTemplate (JdbcTemplate) applicationContext.getBean(jdbcTemplate);/*execute()方法作用 : 执行sql语句*/jdbcTemplate.execute(select * from tb_user);System.out.println(数据查询成功);}/*** 添加*/Test //junit4单元测试public void addStudentTest() {//加载配置文件ApplicationContext applicationContext new ClassPathXmlApplicationContext(com/myh/jdbc/applicationContext.xml);//获取StudentDao实例StudentDao studentDao (StudentDao) applicationContext.getBean(studentDao);//创建Student对象往其中添加数据Student student new Student();student.setUsername(张三);student.setHobby(打篮球);//添加int num studentDao.addStudent(student);if (num 0) {System.out.println(成功插入了 num 条数据!);} else {System.out.println(插入操作执行失败!);}}/*** 更新(修改)*/Testpublic void updateStudentTest() {//加载配置文件ApplicationContext applicationContext new ClassPathXmlApplicationContext(com/myh/jdbc/applicationContext.xml);//获取StudentDao实例StudentDao studentDao (StudentDao) applicationContext.getBean(studentDao);//创建Student对象往其中添加数据Student student new Student();student.setId(1);student.setUsername(小明);student.setHobby(踢足球);//更新(修改)int num studentDao.updateStudent(student);if (num 0) {System.out.println(成功修改了 num 条数据!);} else {System.out.println(修改操作执行失败!);}}/*** 删除*/Testpublic void deleteStudentTest() {//加载配置文件ApplicationContext applicationContext new ClassPathXmlApplicationContext(com/myh/jdbc/applicationContext.xml);//获取StudentDao实例StudentDao studentDao (StudentDao) applicationContext.getBean(studentDao);//删除int num studentDao.deleteStudent(1);if (num 0) {System.out.println(成功删除了 num 条数据!);} else {System.out.println(删除操作执行失败!);}} }query( ) : “查询”返回 “T类型 / List类型” 的结果 query( ) : 对数据库中的数据进行查询操作。 JdbcTemplate类中还提供了大量的query( )方法来处理各种对数据库表的查询操作。 方法说明ListT query ( String sql , RowMapper rowMapper)★★★★★ ( 常用 )执行String类型参数提供的SQL语句并通过 RowMapper 返回一个List类型 的 结果。如 : 可用于查询所有数据。(★★★)ListT query ( String sql , PearesSatementSetter pss , RowMapper rowMapper)根据String 类型参数提供的SQL语句创建 PreparedStatement对象通过RowMapper将结果 / 结果集返回到List中。ListT query ( String sql , Objecr[ ] args , RowMapper rowMapper )使用Obiect[ ]的值来设置SQL语句中的参数值采用 RowMapper回调方法可以直接返回List类型的数值。T queryForObject ( String sql , RowMapper rowMapper , Object… args)★★★★★ ( 常用 )将 args参数绑定到SQL语句中并通过 RowMapper 返回一个Object类型 的 单行记录。如 : 可用于“根据指定id” 查询数据。(★★★)ListT queryForList ( String sql , Object[ ] args , classT )该方法可以 返回多行数据的结果 但必须是返回列表elementType参数返回的是 List元素类型。 query( ) 方法例子 返回 “T类型 / List类型” 的结果 : Student.java package com.myh.jdbc;public class Student {private Integer id; //学生idprivate String username; //用户名private String hobby;/*getter/setter方法*/public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby hobby;}//重写toString()方法Overridepublic String toString() {return Student{ id id , username username \ , hobby hobby \ };} }StudentDao.java (接口) package com.myh.jdbc;import java.util.List;public interface StudentDao { //根据id查询public Student findStudentById(int id);//查询所有public ListStudent findAllStudent(); }StudentDaoImpl.java 实现类 package com.myh.jdbc;import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper;import java.util.List;public class StudentDaoImpl implements StudentDao{ //该类为StudentDao接口的实现类/*** 根据id查询 :* 用queryForObject ( String sql , RowMapper rowMapper , Object...args) 这个方法来进行“根据id”查询数据* 该方法 : 将args参数绑定到sql语句中且通过 RowMapper 返回一个Object类型的“单行记录”。*/Overridepublic Student findStudentById(int id) {//sql语句String sql select * from student where id ?;//创建一个BeanPropertyRowMapper对象RowMapperStudent rowMapper new BeanPropertyRowMapperStudent(Student.class);//调用query()方法查询数据库中的数据Student student this.jdbcTemplate.queryForObject(sql, rowMapper, id); //返回值为一个return student; }/*** 查询所有*/Overridepublic ListStudent findAllStudent() {//sql语句String sql select * from student;//创建 BeanPropertyRowMapper 对象RowMapperStudent rowMapper new BeanPropertyRowMapperStudent(Student.class);ListStudent students this.jdbcTemplate.query(sql, rowMapper); //返回值为list集合该集合中存储一个或多个Student类数据return students;} }applicationContext.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--该配置文件中可以配置Spring JDBC等的配置信息--!-- 1.配置数据源 (与Spring JDBC中的dataSource模块有关) --bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSource!-- 数据库驱动 --property namedriverClassName valuecom.mysql.jdbc.Driver/!-- url --property nameurl valuejdbc:mysql://localhost:3306/spring/!-- 用户名 --property nameusername valueroot/!-- 密码 --property namepassword valueroot//bean!-- 2.配置JDBC模板 / 配置JdbcTemplate (与Spring JDBC中的core模块有关) --bean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplate!-- 默认必须使用数据源 --property namedataSource refdataSource//bean!--定义一个id 为studentDao 的Bean,该Bean用于将jdbcTemplate类注入到studentDao这个实例中因为实例中要用到该对象--bean idstudentDao classcom.myh.jdbc.StudentDaoImplproperty namejdbcTemplate refjdbcTemplate//bean/beansJdbcTemplateTest.java (测试类) package com.myh.jdbc;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.core.JdbcTemplate; import java.util.Iterator; import java.util.List;public class JdbcTemplateTest { /*** 根据id来查询数据*/Testpublic void findStudentByIdTest() {//加载配置文件ApplicationContext applicationContext new ClassPathXmlApplicationContext(com/myh/jdbc/applicationContext.xml);//获取StudentDao实例StudentDao studentDao (StudentDao) applicationContext.getBean(studentDao);//执行 findStudentById()方法Student student studentDao.findStudentById(1);System.out.println(student);}/*** 查询所有数据*/Testpublic void findAllStudentTest() {//加载配置文件ApplicationContext applicationContext new ClassPathXmlApplicationContext(com/myh/jdbc/applicationContext.xml);//获取StudentDao实例StudentDao studentDao (StudentDao) applicationContext.getBean(studentDao);//执行 findAllStudent()方法ListStudent allStudent studentDao.findAllStudent(); //使用“集合”的“迭代器”遍历集合中数据IteratorStudent iterator allStudent.iterator(); while (iterator.hasNext()) {Student next iterator.next();System.out.println(next);}} }
http://www.zqtcl.cn/news/201825/

相关文章:

  • 兼职工厂网站建设万维网的代表网站
  • 企业要建设一个网站需要多少钱网站制作的关键技术
  • 大连住房和城乡建设网站工程公司取名字大全
  • 自建网站投放广告网页设计色彩搭配
  • 网站建站公司哪家价钱合理seo优化是什么职位
  • 德州建设网站网络营销推广方式
  • 网站建设经费预算策划书河南省城乡建设厅网站
  • 校园二手网站开发与设计任务书wordpress 分类名称
  • 高端网站建设那公司好简约网站设计欣赏
  • 域名的网站建设方案书wordpress图片插件
  • 哪个网站做的w7系统好百度网络营销的概念
  • 哪里接单做网站怎么个人网站设计
  • 乐陵seo网站优化网站后期维护是做什么
  • 网站导航html源码网站编辑的工作职能有哪些
  • 品牌网站建设网wordpress密码保护文章
  • 体彩网站开发网站建设备案流程图
  • 云南网站建设天度网页界面设计遵循的原则
  • dede多个网站怎么做wordpress+adsense主题
  • 如何做网站营销江苏网站建设系统服务
  • 公司网站需要备案网络设置怎么设置
  • 网站正在建设中中文模板廊坊网站优化
  • 8免费网站建站网页网站模板
  • 网站开发算法面试滨州建设工程备案网站
  • 扬州网站开发个人做美食视频网站
  • 网站建设属于技术服务吗关于网站建设
  • 网站建设推广好处西安工业设计公司
  • 深圳宝安p2p网站系统的建设百度广告联盟标识
  • 企业网站备案怎么填写东莞市场监督管理局官网
  • 网站建站网站91955内蒙古建设集团招聘信息网站
  • 巴中建设厅网站电话seo是网络优化吗