Spring整合MyBatis-純XML形式(最簡)
前提: 整合MyBatis使用Idea創建Maven工程, 下圖為工程目錄結構

1. 匯入Maven工程相關的坐標 - pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>com.xxx</groupId> <artifactId>spring-mybatis01-zh</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--MyBatis坐標--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!--Mybatis-spring整合坐標--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--druid連接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> <!--MySQL連接坐標--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--spring坐標--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--Spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--Junit-單元測驗坐標--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--Junit-spring的test--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.9.RELEASE</version> </dependency> </dependencies> </project>View Code
2. 創建jdbc的組態檔 - jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=root
View Code
3. Spring核心組態檔 - applicationContext.xml
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--加載perperties組態檔的資訊--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--加載druid資源--> <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> <!--配置service作為spring的bean,注入dao--> <bean id="accountService" class="com.xxx.service.impl.AccountServiceImpl"> <!--注入依賴accountDao--> <property name="accountDao" ref="accountDao"/> </bean> <!--spring整合mybatis后控制的創建連接用的物件--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.xxx.domain"/> </bean> <!--加載mybatis映射配置的掃描,將其作為spring的bean進行管理--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xxx.dao"/> </bean> </beans>View Code
4. Mybatis獲取Mapper組態檔 - AccountDao.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xxx.dao.AccountDao"> <!--配置根據id查詢--> <select id="findById" resultType="account" parameterType="int"> select * from account where id = #{id} </select> <!--配置查詢所有--> <select id="findAll" resultType="account"> select * from account </select> <!--配置保存--> <insert id="save" parameterType="account"> insert into account(name,money)values(#{name},#{money}) </insert> <!--配置洗掉--> <delete id="delete" parameterType="int"> delete from account where id = #{id} </delete> <!--配置根據名稱查詢--> <select id="findByName" resultType="account" parameterType="string"> select * from account where name = #{name} </select> <!--配置更新--> <update id="update" parameterType="account"> update account set name=#{name},money=#{money} where id=#{id} </update> </mapper>View Code
5. dao - AccountDao.java
package com.xxx.dao; import com.xxx.domain.Account; import java.util.List; public interface AccountDao { void save(Account account); void delete(Integer id); void update(Account account); List<Account> findAll(); Account findById(Integer id); }View Code
6. domain - Account.java
package com.xxx.domain; import java.io.Serializable; public class Account implements Serializable { private Integer id; private String name; private Double money; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }View Code
7. service - AccountService.java
package com.xxx.service; import com.xxx.domain.Account; import java.util.List; public interface AccountService { void save(Account account); void delete(Integer id); void update(Account account); List<Account> findAll(); Account findById(Integer id); }View Code
8. service.impl - AccountServiceImpl.java
package com.xxx.service.impl; import com.xxx.dao.AccountDao; import com.xxx.domain.Account; import com.xxx.service.AccountService; import java.util.List; public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public void save(Account account) { accountDao.save(account); } public void update(Account account){ accountDao.update(account); } public void delete(Integer id) { accountDao.delete(id); } public Account findById(Integer id) { return accountDao.findById(id); } public List<Account> findAll() { return accountDao.findAll(); } }View Code
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/53074.html
標籤:Java
