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

什么网站可以做图赚钱给网站做seo的价格

什么网站可以做图赚钱,给网站做seo的价格,长春建设信息网站,bootstrap 网站源代码Spring 集成 MyBatis 将 MyBatis 与 Spring 进行整合,主要解决的问题就是将 SqlSessionFactory 对象交由 Spring 来管理。所以该整合,只需要将 SqlSessionFactory 的对象生成器SqlSessionFactoryBean 注册在 Spring 容器中,再将其注入给 Dao…

Spring 集成 MyBatis

将 MyBatis 与 Spring 进行整合,主要解决的问题就是将 SqlSessionFactory 对象交由 Spring 来管理。所以该整合,只需要将 SqlSessionFactory 的对象生成器SqlSessionFactoryBean 注册在 Spring 容器中,再将其注入给 Dao 的实现类即可完成整合。

实现 Spring 与 MyBatis 的整合常用的方式:扫描的 Mapper 动态代理

Spring 像插线板一样,mybatis 框架是插头,可以容易的组合到一起。

插线板 spring 插上 mybatis,两个框架就是一个整体。

1、MySQL 创建数据库 db1,新建表 student2

 

2、maven 依赖 pom.xml

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.26</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.3.26</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.26</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.11</version></dependency><!-- Spring整合MyBatis的依赖 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.1.0</version></dependency><!-- --------------------- --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.29</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency>
</dependencies>	<!--插件:-->
<build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins>
</build>

3、定义实体类 Student

 代码如下:

package com.ambow.pojo;import lombok.Data;@Data
public class Student {private int id;private String name;private int age;}

4、定义 StudentDao 接口

 代码如下:

package com.ambow.dao;import com.ambow.pojo.Student;import java.util.List;public interface StudentDao {int insertStudent(Student student);int updateStudent(Student student);int deleteStudent(int id);Student selectStudentById(int id);List<Student> selectAllStudents();
}

5、定义映射文件 mapper

在 Dao 接口的包中创建 MyBatis 的映射文件 mapper,命名与接口名相同,本例为 StudentDao.xml

mapper 中的 namespace 取值也为 Dao 接口 的全限定性名。

  代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ambow.dao.StudentDao"><insert id="insertStudent">insert into student2 values(null,#{name},#{age})</insert><update id="updateStudent">update student2 set name = #{name},age = #{age} where id = #{id}</update><delete id="deleteStudent">delete from student2 where id = #{id}</delete><select id="selectStudentById" resultType="com.ambow.pojo.Student">select * from student2 where id = #{id}</select><select id="selectAllStudents" resultType="com.ambow.pojo.Student">select * from student2</select></mapper>

6、定义 Service 接口和实现类

接口定义:

  代码如下:

package com.ambow.service;import com.ambow.pojo.Student;import java.util.List;public interface StudentService {int addStudent(Student student);int modifyStudent(Student student);int removeStudent(int id);Student findStudentById(int id);List<Student> findAllStudents();
}

实现类定义:

  代码如下:

package com.ambow.service.impl;import com.ambow.dao.StudentDao;
import com.ambow.pojo.Student;
import com.ambow.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;@Service("studentService")
public class StudentServiceImpl implements StudentService {//引入StudentDao的对象@Autowiredprivate StudentDao studentDao;public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}@Overridepublic int addStudent(Student student) {return studentDao.insertStudent(student);}@Overridepublic int modifyStudent(Student student) {return studentDao.updateStudent(student);}@Overridepublic int removeStudent(int id) {return studentDao.deleteStudent(id);}@Overridepublic Student findStudentById(int id) {return studentDao.selectStudentById(id);}@Overridepublic List<Student> findAllStudents() {return studentDao.selectAllStudents();}
}

7、定义 MyBatis 主配置文件

在 src 下定义 MyBatis 的主配置文件,命名为 mybatis.xml

这里有两点需要注意:

(1)主配置文件中不再需要数据源的配置了。因为数据源要交给 Spring 容器 来管理了。

(2)这里对 mapper 映射文件的注册,使用标签,即只需给出 mapper 映射文件所在的包即可。因为 mapper 的名称与 Dao 接口名相同, 可以使用这种简单注册方式。

这种方式的好处是:若有多个映射文件,这里的 配置也是不用改变的。当然,也可使用原来的标签方式。

 

8 、修改 Spring 配置文件

(1) 数据源的配置(掌握)

使用 JDBC 模板,首先需要配置好数据源,数据源直接以 Bean 的形式配置 在 Spring 配置文件中。根据数据源的不同,其配置方式不同:

Druid 数据源 DruidDataSource Druid 是阿里的开源数据库连接池。是 Java 语言中最好的数据库连接 池。Druid 能够提供强大的监控和扩展功能。Druid 与其他数据库连接池的 最大区别是提供数据库的

官网:GitHub - alibaba/druid: 阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池 - GitHub - alibaba/druid: 阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池https://github.com/alibaba/druid

使用地址:Home · alibaba/druid Wiki · GitHub阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池 - Home · alibaba/druid Wikihttps://github.com/alibaba/druid/wiki/

常见问题

配置连接池:

  Spring 配置文件:

  代码如下:

    <!--1.DataSource--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean>

(2) 从属性文件读取数据库连接信息

为了便于维护,可以将数据库连接信息写入到属性文件中,使 Spring 配置 文件从中读取数据。

属性文件名称自定义,但一般都是放在 src 下。

  代码如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.2.111:3306/db1
jdbc.username=root
jdbc.password=Mysql666!

Spring 配置文件从属性文件中读取数据时,需要在的 value 属性中使用${ },将在属性文件中定义的 key 括起来,以引用指定属性的值。

该属性文件若要被 Spring 配置文件读取,其必须在配置文件中进行注册。 使用<context>标签。

<context:property-placeholder />方式(掌握)

该方式要求在 Spring 配置文件头部加入 spring-context.xsd 约束文件

<context:property-placeholder />标签中有一个属性 location,用于指定属 性文件的位置。

 (3) 注册 SqlSessionFactoryBean

 

代码如下: 

    <!--2.注册SqlSessionFactoryBean--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="typeAliasesPackage" value="com.ambow.pojo" /><property name="mapperLocations" value="classpath:com/ambow/dao/*.xml" /></bean>

(4) 定义 Mapper 扫描配置器 MapperScannerConfigurer

Mapper 扫描配置器 MapperScannerConfigurer 会自动生成指定的基本 包中 mapper 的代理对象。该 Bean 无需设置 id 属性。basePackage 使用分 号或逗号设置多个包。

 

 代码如下:

    <!--3.mapper的扫描配置器 -> 生成mapper的代理对象--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><property name="basePackage" value="com.ambow.dao" /></bean>

9、向 Service 注入接口名

向 Service 注入 Mapper 代理对象时需要注意,由于通过 Mapper 扫描配置器 MapperScannerConfigurer 生成的 Mapper 代理对象没有名称,所以在 向 Service 注入 Mapper 代理时,无法通过名称注入。但可通过接口的简单类名注入,因为生成的是这个 Dao 接口的对象。

 

 代码如下:

    <!--4.向service层注入Dao-->
<!--    <bean id="studentService" class="com.ambow.service.impl.StudentServiceImpl"><property name="studentDao" ref="studentDao" /></bean>-->

10、Spring 配置文件全部配置

 代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--整合:把MyBatis中的核心对象,放到Spring容器--><!--引入属性配置文件--><context:property-placeholder location="classpath:jdbc.properties" /><context:component-scan base-package="com.ambow.service" /><!--1.DataSource--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!--2.注册SqlSessionFactoryBean--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="typeAliasesPackage" value="com.ambow.pojo" /><property name="mapperLocations" value="classpath:com/ambow/dao/*.xml" /></bean><!--3.mapper的扫描配置器 -> 生成mapper的代理对象--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><property name="basePackage" value="com.ambow.dao" /></bean><!--4.向service层注入Dao-->
<!--    <bean id="studentService" class="com.ambow.service.impl.StudentServiceImpl"><property name="studentDao" ref="studentDao" /></bean>--><!--注入的过程--><!--dataSource -> SqlSessionFactoryBean -> MapperScannerConfigurer(生成Dao代理对象) -> studentService--></beans>

项目整体结构如下图:

 运行测试类test02:

 

运行测试类test03:

 

http://www.zhongyajixie.com/news/6974.html

相关文章:

  • 网站建设和软件开发安徽网站优化
  • 淘宝内部优惠券网站怎么做网页模板大全
  • joomla 网站建设教程性价比高seo的排名优化
  • google关键词优化西安seo网站管理
  • 酷站是什么网站建站优化推广
  • 不备案网站怎么做淘宝客搜索推广开户
  • 揭阳网站建设搜索引擎谷歌入口
  • wordpress网站嵌入商城快速排名刷
  • 网站备案查询工信部官网口碑营销例子
  • 手机版qq电脑登录网页入口windows7优化大师下载
  • 创新网站建设厦门网站优化
  • 浙江大成建设集团有限公司网站嘉兴seo优化
  • 免费做简历的软件网站公司网站首页设计
  • wordpress上传sh文件武汉seo关键词排名优化
  • 深圳网站建设优化服务促销式软文案例
  • o2o网站建设多少钱百度指数的特点
  • 建设资格执业注册中心网站国内最好用免费建站系统
  • 网站程序开发百度竞价推广是什么工作
  • 互联网平台怎么建立杭州seo推广排名稳定
  • 导航 网站 分析太原网站建设制作
  • 长沙 外贸网站建设公司排名罗湖区seo排名
  • 铜仁市城乡住房与建设局网站友情链接多少钱一个
  • 眼科医院网站建设方案seo零基础教学
  • 做网站太麻烦了辽宁和生活app下载安装
  • 国外哪些网站有黄图百度竞价价格查询
  • 怎么提高百度权重seo排名点击手机
  • 做自己的批发网站需要什么营业执照it培训机构排名及学费
  • 邯郸网站seo优化多久能上排名
  • 做网站百科株洲网站设计
  • 淘宝美工网站怎么做打开百度首页