注解式
首先导入依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.0</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency>
entity实体类:
public class IAccount implements Serializable { private String accountid; //卡号 private String accountname; //所属人姓名 private double balance; //卡内余额 public String getAccountid() { return accountid; } public void setAccountid(String accountid) { this.accountid = accountid; } public String getAccountname() { return accountname; } public void setAccountname(String accountname) { this.accountname = accountname; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } }
Dao接口层
@Repositorypublic interface IAccountDao { public List<IAccount> getAllAccount(); }
Dao.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"> <!--小配置根节点 namespace代表命名空间--> <mapper namespace="com.mybatis_spring.dao.IAccountDao"> <select id="getAllAccount" resultType="IAccount"> select * from accounts </select> </mapper>
Service业务接口层:
public interface IAccountService { public List<IAccount> getAllAccount(); }
ServiceImpl业务接口实现层:
@Service("iAccountService")
public class IAccountServiceImpl implements IAccountService {
@Resource private IAccountDao iAccountDao; @Override public List<IAccount> getAllAccount() { return iAccountDao.getAllAccount(); } }
jdbc.properties文件:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:/ jdbc.username=root jdbc.password=root
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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描注解--> <context:component-scan base-package="com.mybatis_spring"/> <!--加载配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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> <!--配置SqlSessionFactoryBean--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--加载Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--扫描dao层--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis_spring.dao.IAccountDao"/> </bean> <!-- 0<!–注入Dao层–> <bean id="iAccountDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mybatis_spring.dao.IAccountDao"/> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/> </bean> <!–<!–Service层–>–> <bean id="iAccountService" class="com.mybatis_spring.service.Impl.IAccountServiceImpl"> <property name="iAccountDao" ref="iAccountDao"/> </bean>--> </beans>
测试:
public class IAccountTest { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); IAccountService iBankService = (IAccountService)ctx.getBean("iAccountService"); List<IAccount> iAccounts = iBankService.getAllAccount(); for(IAccount iAccount:iAccounts){ System.out.println(iAccount.getAccountname()); } } }
注解式
entity实体类:
public class IAccount implements Serializable { private String accountid; //卡号 private String accountname; //所属人姓名 private double balance; //卡内余额 public String getAccountid() { return accountid; } public void setAccountid(String accountid) { this.accountid = accountid; } public String getAccountname() { return accountname; } public void setAccountname(String accountname) { this.accountname = accountname; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } }
Dao层接口类:
public interface IAccountDao { public List<IAccount> getAllAccount(); }
Dao层小配置文件:
<?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"> <!--小配置根节点 namespace代表命名空间--> <mapper namespace="com.mybatis_spring.dao.IAccountDao"> <select id="getAllAccount" resultType="IAccount"> select * from accounts </select> </mapper>
Service接口类:
public interface IAccountService { public List<IAccount> getAllAccount(); }
Service接口实现类:
public class IAccountServiceImpl implements IAccountService { @Resource private IAccountDao iAccountDao; @Override public List<IAccount> getAllAccount() { return iAccountDao.getAllAccount(); } }
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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描注解--> <context:component-scan base-package="com.mybatis_spring"/> <!--加载配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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> <!--配置SqlSessionFactoryBean--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--加载Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--扫描dao层--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis_spring.dao.IAccountDao"/> </bean> <!-- 0<!–注入Dao层–> <bean id="iAccountDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mybatis_spring.dao.IAccountDao"/> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/> </bean> <!–<!–Service层–>–> <bean id="iAccountService" class="com.mybatis_spring.service.Impl.IAccountServiceImpl"> <property name="iAccountDao" ref="iAccountDao"/> </bean>--> </beans>
测试:
public class IAccountTest { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); IAccountService iBankService = (IAccountService)ctx.getBean("iAccountService"); List<IAccount> iAccounts = iBankService.getAllAccount(); for(IAccount iAccount:iAccounts){ System.out.println(iAccount.getAccountname()); } } }
原文地址:https://www.cnblogs.com/haohanwuyin/p/11824103.html
时间: 2024-11-06 10:01:46