Spring統合MyBatis(半注半xml)
Spring統合MyBatis(半注半xml)
1インポート依存
1.1導入する依存パッケージの紹介
1:Spring容器の依存
2:Spring操作JDBCの依存
3:MyBatisの依存
4:SpringとMyBatisの統合の依存
5:ドルイド接続池の依存
6:Junnitのテスト依存性
7:Lombook依存
8:Springのテスト依存性
2.1 MyBatis接続データベース配置ファイル
2.2.1配置が必要なxml手順
1:データベースの空きプロファイルをロードする(構成context制約が必要)
2:プロファイルのパラメータ(setメソッド取得)を取得する
3:接続池を配置し、mybatisの工場を配置する
3.1:接続パラメータを教え、set方式でロシア籍にパラメータを注入して食べます。
3.2:関連mybatisコア配置ファイル(今後使用されるかもしれません)
3.3:domainの実体類に別名をつける
4:Springを私達の代わりにdao層を作って容器に入れます。
5:Springにserviceにdaoを注入してください。
3.1データアクセス層
3.2.1インターフェースにおける方法
1:@Service:3つの概念をより具現化するために
2:@Autowried:Srping容器はSpring工場に対象を作成してくれるように教えてください。もし複数の実現類があれば、まだ未確定です。
通常は注釈@Qualfierの使用に協力します。
@Qualfier:id注入による
3.3.1:注釈紹介
1:@RunWith:本質的には自分でRunnerオブジェクトを実現しました。
1.1:メリット1:Springを手動で起動する必要がない
1.2:メリット2:Junnitテストクラスで@AutoWried方式でオブジェクトを注入でき、直接調整ができます。
2:@contextConfiguration:フレーム起動入口:xmlプロファイル起動
3:@AuroWried:注入により、IOC(容器)からオブジェクトを取得する
1インポート依存
1.1導入する依存パッケージの紹介
1:Spring容器の依存
2:Spring操作JDBCの依存
3:MyBatisの依存
4:SpringとMyBatisの統合の依存
5:ドルイド接続池の依存
6:Junnitのテスト依存性
7:Lombook依存
8:Springのテスト依存性
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<groupId>com.aoshengroupId>
<artifactId>MybatisAndSpringartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.20version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.8version>
dependency>
dependencies>
project>
2各種プロファイル2.1 MyBatis接続データベース配置ファイル
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1
jdbc.username=root
jdbc.password=root
2.2コアSpring xmlプロファイル2.2.1配置が必要なxml手順
1:データベースの空きプロファイルをロードする(構成context制約が必要)
2:プロファイルのパラメータ(setメソッド取得)を取得する
3:接続池を配置し、mybatisの工場を配置する
3.1:接続パラメータを教え、set方式でロシア籍にパラメータを注入して食べます。
3.2:関連mybatisコア配置ファイル(今後使用されるかもしれません)
3.3:domainの実体類に別名をつける
4:Springを私達の代わりにdao層を作って容器に入れます。
5:Springにserviceにdaoを注入してください。
<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
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:property-placeholder location="classpath:db.properties" />
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="configLocation" value="classpath:sqlMapConfig.xml">property>
<property name="typeHandlersPackage" value="com.aoshen.domain"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.aoshen.dao">property>
bean>
<context:component-scan base-package="com.aoshen.service">context:component-scan>
beans>
3データアクセス層、業務層半xml半注開発3.1データアクセス層
package com.aoshen.dao;
import com.aoshen.domain.Account;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface AccountDao {
//
@Insert("insert into account values(null,#{name},#{money})")
int insertAccount(Account account);
// id
@Select("select * from account where id = #{id}")
Account findAccountBuId(Integer id);
//
@Delete("delete from account where id = #{id}")
void deleteAccount(@Param("id") Integer id);
//
@Update("update account set name = #{name},money = #{money} where id = #{id}")
void updateAccount(Account account);
//
@Select("select * from account")
List<Account> findAllByAccount();
}
3.2業務層3.2.1インターフェースにおける方法
package com.aoshen.service;
import com.aoshen.domain.Account;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface AccountService {
//
int insertAccount(Account account);
// id
Account findAccountBuId(Integer id);
//
void deleteAccount(Integer id);
//
void updateAccount(Account account);
//
List<Account> findAllByAccount();
}
3.2.2インターフェースの実現類1:@Service:3つの概念をより具現化するために
2:@Autowried:Srping容器はSpring工場に対象を作成してくれるように教えてください。もし複数の実現類があれば、まだ未確定です。
通常は注釈@Qualfierの使用に協力します。
@Qualfier:id注入による
package com.aoshen.service.impl;
import com.aoshen.dao.AccountDao;
import com.aoshen.domain.Account;
import com.aoshen.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
public int insertAccount(Account account) {
return accountDao.insertAccount(account);
}
@Override
public Account findAccountBuId(Integer id) {
return accountDao.findAccountBuId(id);
}
@Override
public void deleteAccount(Integer id) {
accountDao.deleteAccount(id);
}
@Override
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
@Override
public List<Account> findAllByAccount() {
return accountDao.findAllByAccount();
}
}
3.3試験類3.3.1:注釈紹介
1:@RunWith:本質的には自分でRunnerオブジェクトを実現しました。
1.1:メリット1:Springを手動で起動する必要がない
1.2:メリット2:Junnitテストクラスで@AutoWried方式でオブジェクトを注入でき、直接調整ができます。
2:@contextConfiguration:フレーム起動入口:xmlプロファイル起動
3:@AuroWried:注入により、IOC(容器)からオブジェクトを取得する
package com.aoshen.service;
import com.aoshen.domain.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class AccountServiceTest {
@Autowired
private AccountService accountService;
@Test
public void insertAccount() {
Account account = new Account(null," ",131313D);
accountService.insertAccount(account);
}
@Test
public void findAccountBuId() {
Account accountBuId = accountService.findAccountBuId(2);
System.out.println(accountBuId);
}
@Test
public void deleteAccount() {
accountService.deleteAccount(4);
}
@Test
public void updateAccount() {
Account account = accountService.findAccountBuId(2);
account.setName(" ");
account.setMoney(23333D);
accountService.updateAccount(account);
}
@Test
public void findAllByAccount() {
List<Account> list = accountService.findAllByAccount();
list.forEach(System.out::println);
}
}