1つは、SpringがMyBatisを統合することです。
- 概念:
MyBatisをSpringと統合する場合、解決すべき主な問題は、SqlSessionFactoryオブジェクトをSpringに渡して管理することです。したがって、この統合では、SqlSessionFactoryオブジェクトジェネレーターSqlSessionFactoryBeanをSpringコンテナーに登録し、それをDaoの実装クラスに挿入するだけで統合を完了できます。
- 使用したテクノロジー:IoC
- IoCを使用する理由:
オブジェクトを作成できるため、フレームワークのようにmybatisとspringを統合します。
mybatisを使用する手順を思い出してください。
1)daoインターフェースStudentDaoを
定義します2)マッパーファイルStudentDao.xmlを定義します
3)メイン構成ファイルmybatis.xmlを定義します
4)daoプロキシオブジェクトを作成します
StudentDao dao = SqlSession.getMapper(StudentDao.class);
List<Student> students = dao.selectStudents();
ここでdaoオブジェクトを使用するには、getMapper()メソッドを使用する必要があります。
では、getMapper()メソッドを作成するにはどのような条件が必要ですか?
1.SqlSessionFactoryオブジェクトを作成します。2。SqlSessionFactory
のopenSession()メソッドを使用してSqlSessionオブジェクトを取得します。
主な構成ファイル:
1.データベース情報
<environment id="mydev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库的驱动类名-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--连接数据库的url字符串-->
<property name="url" value="jdbc:mysql://localhost:3306/springdb"/>
<!--访问数据库的用户名-->
<property name="username" value="root"/>
<!--密码-->
<property name="password" value="123456"/>
</dataSource>
2.マッパーファイルの場所
<mappers>
<mapper resource="com/bjpowernode/dao/StudentDao.xml"/>
<!--<mapper resource="com/bjpowernode/dao/SchoolDao.xml" />-->
</mappers>
ここでは、別の接続プールを使用してデフォルトの組み込みmybatisを置き換え、接続プールをSpringコンテナに渡して作成します。
上記の説明により、Springに次のオブジェクトを作成させる必要があり
ます。1。独立した接続プールクラスオブジェクト、Aliのドルイド接続プールを使用
2.SqlSessionFactoryオブジェクト3.daoオブジェクトを
作成
説明する例を次に示します。
(1)Mavenに次の依存関係を追加します
!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!--做spring事务依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--mybatis-spring集成依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes>
<!--包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<!--指定jdk版本-->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
(2)エンティティクラスStudentを作成します
public class Student {
//属性名和列名一样
private Integer id;
private String name;
private String email;
private Integer age;
//这里省略set方法
(3)StudentDaoインターフェースを作成します
public interface StudentDao {
int insertStudent(Student student);
List<Student> selectStudent();
}
(4)マッピングファイルStudentDao.xmlを作成します
<mapper namespace="com.hcz.dao.StudentDao">
<insert id="insertStudent">
insert into student values (#{id},#{name},#{email},#{age})
</insert>
<select id="selectStudent" resultType="com.hcz.entity.Student">
select * from student order by id desc
</select>
</mapper>
(5)メイン設定ファイルmybatis.xmlを作成します
<configuration>
<!--mybatis.xml 文件加入日志配置,可以在控制台输出执行的 sql 语句和参数-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--设置别名-->
<typeAliases>
<package name="com.hcz.entity"/>
</typeAliases>
<!--sql映射文件的位置-->
<mappers>
<!--告诉mybatis要执行的sql语句的位置-->
<package name="com.hcz.dao"/>
</mappers>
</configuration>
注:
データソース構成は、メイン構成ファイルでは不要になりました。データソースはSpringコンテナで管理する必要があるためです。
(6)サービスインターフェイスとその実装クラスを作成します
public interface StudentService {
int addStudent(Student student);
List<Student> queryStudent();
}
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
int nums = studentDao.insertStudent(student);
return nums;
}
@Override
public List<Student> queryStudent() {
List<Student> students = studentDao.selectStudent();
return students;
}
}
(7)Spring構成ファイルapplicationContext.xmlを作成します
構成は主に次の4つの側面から行われます
。1)データソース
2)SqlSessionFactoryの登録
3)マッパースキャナーの登録
4)カスタムサービスの宣言
1)データソースを構成します(DruidデータソースDruidDataSource)
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!--set注入给DruidDataSource提供连接数据库信息 -->
<!-- 使用属性配置文件中的数据,语法 ${key} -->
<property name="url" value="${jdbc.url}" /><!--setUrl()-->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="20" />
</bean>
2)SqlSessionFactoryを登録します
<!--注册 SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--set注入,把数据库连接池付给了dataSource属性-->
<property name="dataSource" ref="myDataSource" />
<!--mybatis主配置文件的位置
configLocation属性是Resource类型,读取配置文件
它的赋值,使用value,指定文件的路径,使用classpath:表示文件的位置
-->
<property name="configLocation" value="classpath:mybatis.xml" />
</bean>
3)マッパースキャナーを登録する
<!--注册Mapper扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定SqlSessionFactory对象的id-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!--指定包名, 包名是dao接口所在的包名-->
<property name="basePackage" value="com.hcz.dao"/>
</bean>
分析:
MapperScannerConfigurerは、このパッケージ内のすべてのインターフェイスをスキャンし、インターフェイスごとにgetMapper()メソッドを1回実行して、各インターフェイスのdaoオブジェクトを取得します。作成されたdaoオブジェクトはスプリングコンテナに入れられます。daoオブジェクトのデフォルト名は、インターフェイス名の最初の文字の小文字です。
4)カスタムサービス実装クラスを宣言します
<!--声明service-->
<bean id="studentService" class="com.hcz.service.Impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
</bean>
解決策:
マッパースキャナーによって生成されたマッパープロキシオブジェクトには名前がないため、マッパープロキシをサービスに挿入する場合、名前を介して挿入することはできませんが、オブジェクトがインターフェイスの単純なクラス名を介して挿入することはできます。 Daoインターフェースのが生成されます。