1:Spring容器的依赖
2:Spring操作JDBC的依赖
3:MyBatis的依赖
4:Spring与MyBatis整合的依赖
5:Druid连接池的依赖
6:Junit的测试依赖
7:Lombok依赖
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>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1
jdbc.username=root
jdbc.password=root
1:加载数据库的饿配置文件(需要配置context约束)
2:获取配置文件中的参数(set方法获取)
3:配置连接池,将mybatis的工厂配置进来
3.1:告诉连接参数,通过set方式把参数注入进联俄籍吃中
3.2:关联mybatis核心配置文件(以后可能会用到)
3.3:给domain的实体类起个别名
4:让Spring来代替我们自己创建dao层,并放到容器中
5:让Spring帮我们把dao注入到service中(set或构造函数方式注入)
<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>
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();
}
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();
}
1:@Service:为了更能体现三层概念
2:@Autowried:告诉Srping容器让Spring工厂来帮我创建对象,如果有多个实现类且无发确定
通常配合注解@Qualifier的使用
@Qualifier:通过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();
}
}
1:@RunWith:本质上是自己实现了一个Runner对象
1.1:好处1:不需要手动启动Spring
1.2:好处2:可以在Junit测试类中使用@AutoWried方式注入对象,可以直接进行调试
2:@contextConfigguration:框架启动入口: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);
}
}