导入数据库
-
调用图形化工具执行sql文件
-
数据库表如下:
创建项目
【https://blog.csdn.net/qq_41209886/article/details/109216349】
- 父工程export-parent
- 子工程export-dao
export-domain
export-service
export-webmanager
准备实体类domain
import java.util.Date;
//企业实体类
public class Company {
public Company() {
}
@Override
public String toString() {
return "Company{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", expirationDate=" + expirationDate +
", address='" + address + '\'' +
", licenseId='" + licenseId + '\'' +
", representative='" + representative + '\'' +
", phone='" + phone + '\'' +
", companySize='" + companySize + '\'' +
", industry='" + industry + '\'' +
", remarks='" + remarks + '\'' +
", state=" + state +
", balance=" + balance +
", city='" + city + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getExpirationDate() {
return expirationDate;
}
public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLicenseId() {
return licenseId;
}
public void setLicenseId(String licenseId) {
this.licenseId = licenseId;
}
public String getRepresentative() {
return representative;
}
public void setRepresentative(String representative) {
this.representative = representative;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCompanySize() {
return companySize;
}
public void setCompanySize(String companySize) {
this.companySize = companySize;
}
public String getIndustry() {
return industry;
}
public void setIndustry(String industry) {
this.industry = industry;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
/**
* 对象唯一标记,对应数据库主键
*/
private String id;
/**
* 公司名称
*/
private String name;
/**
* 到期时间
*/
private Date expirationDate;
/**
* 公司地址
*/
private String address;
/**
* 营业执照
*/
private String licenseId;
/**
* 法人代表
*/
private String representative;
/**
* 公司电话
*/
private String phone;
/**
* 公司规模
*/
private String companySize;
/**
* 所属行业
*/
private String industry;
/**
* 备注
*/
private String remarks;
/**
* 状态
*/
private Integer state;
/**
* 当前余额
*/
private Double balance;
/**
* 城市
*/
private String city;
}
dao层Spring整合MyBatis进行数据库访问(export_dao子工程)
检查 pom.xml
有没有spring与mybatis的整合包
export_dao\pom.xml
<!-- 子工程继承父工程,可以使用父工程中的配置-->
<!-- A子工程 依赖 B子工程,可以使用里面的B子工程 java类-->
<dependencies>
<dependency>
<artifactId>export_domain</artifactId>
<groupId>com.smp</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
(1)properties/db.properties
#key=value
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/saas-export?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
(2)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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--读取db.properties文件-->
<context:property-placeholder location="classpath:properties/db.properties"></context:property-placeholder>
<!--1.配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--2.配置Spring整合Mybatis *** 由Spring创建SqlSessionFactory对象-->
<!--2.1 配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- com.smp.domain.company.Company company-->
<property name="typeAliasesPackage" value="com.smp.domain"/>
</bean>
<!--2.2 配置Dao接口所在包 动态代理 session.getMapper(Dao.class)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定Dao接口所在包-->
<property name="basePackage" value="com.smp.dao"/>
</bean>
</beans>
(3)定义ICompanyDao
import com.smp.domain.company.Company;
import java.util.List;
public interface ICompanyDao {
//查询所有的公司纪录
//select * from ss_company;
List<Company> findAll();
}
(4)定义ICompanyDao.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.smp.dao.company.ICompanyDao">
<!--<select id="findAll" resultType="company">
select
id,
name ,
expiration_date as expirationDate ,
address,
license_id as licenseId ,
representative ,
phone ,
company_size as companySize ,
industry ,
remarks ,
state,
balance ,
city
from ss_company
</select>-->
<!--//查询所有的公司纪录
//select * from ss_company;
List<Company> findAll();-->
<resultMap id="companyMap" type="company">
<id column="id" property="id"/>
<result column="expiration_date" property="expirationDate"/>
<result column="license_id" property="licenseId"/>
<result column="company_size" property="companySize"/>
</resultMap>
<select id="findAll" resultMap="companyMap">
select * from ss_company
</select>
</mapper>
(5)测试
src\test\java\com\smp\dao\company\ICompanyDaoTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-dao.xml")
public class ICompanyDaoTest {
@Autowired
ICompanyDao iCompanyDao;
@Test
public void findAll(){
System.out.println(iCompanyDao);
List<Company> list=iCompanyDao.findAll();
System.out.println(list);
}
}
测试结果
service依赖dao
测试读取dao的信息,配置事务管理(export_system_service子工程)
子工程export_system_service依赖子工程export_domain
关于classpath路径读取的使用
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
classpath
: 加载当前maven工程的resources目录下的配置文件
classpath*
: 加载当前maven工程及其依赖工程的resources目录下的配置文件
applicationContext-*.xml
: 读取所有符合规则的文件
(1)TestCompanyService.java测试
src\test\java\com\smp\service\company
//添加spring单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-*.xml")
public class TestCompanyService {
@Autowired
ICompanyService service;
@Test
public void test01(){
//编写业务逻辑测试
//左边接口,右边实现类
//ICompanyService service= new CompanyServiceImpl();
List<Company> list=service.findAll();
System.out.println(list);
}
}
(2)applicationContext-tx.xml事务管理
src\main\resources\spring
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.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">
<!--扫描Service实现类-->
<context:component-scan base-package="com.smp.service"/>
<!--Spring声明式事务(底层就是AOP): 三步曲-->
<!--1.配置事务管理器:管理事务:DataSource.Connection.commit() rollback()方法 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--2.配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<!--配置事务细节特征-->
<tx:attributes>
<!--查询方法,使用默认的隔离级别 及 SUPPORTS传播行为-->
<tx:method name="find*" isolation="DEFAULT" propagation="SUPPORTS"/>
<tx:method name="query*" isolation="DEFAULT" propagation="SUPPORTS"/>
<tx:method name="select*" isolation="DEFAULT" propagation="SUPPORTS"/>
<tx:method name="get*" isolation="DEFAULT" propagation="SUPPORTS"/>
<!--增删改方法,使用默认的隔离级别 及 REQUIRED传播行为-->
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--3.配置事务切面: 切面=通知+切入点-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pt" expression="execution(* com.smp.service.*.impl.*.*(..))"/>
<!--切面=通知+切入点-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
(3)ICompanyService.java接口
src\main\java\com\smp\service\company
public interface ICompanyService {
List<Company> findAll();
}
(4)CompanyServiceImpl.java实现类
src\main\java\com\smp\service\company\impl
@Service
public class CompanyServiceImpl implements ICompanyService {
@Autowired
ICompanyDao iCompanyDao;
public List<Company> findAll() {
return iCompanyDao.findAll();
}
}
(5)配置spring事务时遇到了dataSource错误
applicationContext.xml无法解析数据源dataSource
产生的原因是spring的配置文件没有导入,需要手动导入
- 点击File——》Project structure——》
- Moudles–>点击+号–>勾选上配置文件–>ok–>apply–>ok即可