作业一
1.创建工程,导入相关的jar包
2.创建pojo包
Provider.java
package com.openlab.pojo;
import java.util.Date;
public class Provider {
private Integer id;
private String proCode;
private String proName;
private String proDesc;
private String proContact;
private String proPhone;
private String proAddress;
private String proFax;
private Integer createdBy;
private Date creationDate;
private Date modifyDate;
private Integer modifyBy;
private String companyLicPicPath;
private String orgCodePicPath;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
}
public String getProContact() {
return proContact;
}
public void setProContact(String proContact) {
this.proContact = proContact;
}
public String getProPhone() {
return proPhone;
}
public void setProPhone(String proPhone) {
this.proPhone = proPhone;
}
public String getProAddress() {
return proAddress;
}
public void setProAddress(String proAddress) {
this.proAddress = proAddress;
}
public String getProFax() {
return proFax;
}
public void setProFax(String proFax) {
this.proFax = proFax;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public String getCompanyLicPicPath() {
return companyLicPicPath;
}
public void setCompanyLicPicPath(String companyLicPicPath) {
this.companyLicPicPath = companyLicPicPath;
}
public String getOrgCodePicPath() {
return orgCodePicPath;
}
public void setOrgCodePicPath(String orgCodePicPath) {
this.orgCodePicPath = orgCodePicPath;
}
@Override
public String toString() {
return "Provider{" +
"id=" + id +
", proCode='" + proCode + '\'' +
", proName='" + proName + '\'' +
", proDesc='" + proDesc + '\'' +
", proContact='" + proContact + '\'' +
", proPhone='" + proPhone + '\'' +
", proAddress='" + proAddress + '\'' +
", proFax='" + proFax + '\'' +
", createdBy=" + createdBy +
", creationDate=" + creationDate +
", modifyDate=" + modifyDate +
", modifyBy=" + modifyBy +
", companyLicPicPath='" + companyLicPicPath + '\'' +
", orgCodePicPath='" + orgCodePicPath + '\'' +
'}';
}
}
3.配置mybatis-config配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.openlab.pojo"></package>
</typeAliases>
</configuration>
4.创建dao包
ProviderDao.java
package com.openlab.dao;
import com.openlab.pojo.Provider;
import java.util.List;
public interface ProviderDao {
List<Provider> selectAllProvider();
}
ProviderMapper.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.openlab.dao.ProviderDao">
<select id="selectAllProvider" resultType="Provider">
select * from smbms_provider
</select>
</mapper>
5.创建dao.impl包
ProviderDaoImpl.java
package com.openlab.dao.impl;
import com.openlab.dao.ProviderDao;
import com.openlab.pojo.Provider;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class ProviderDaoImpl implements ProviderDao {
private SqlSessionTemplate sqlSessionTemplate;
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<Provider> selectAllProvider() {
return sqlSessionTemplate.selectList("com.openlab.dao.ProviderDao.selectAllProvider");
}
}
6.创建service包
ProviderService.java
package com.openlab.service;
import com.openlab.pojo.Provider;
import java.util.List;
public interface ProviderService {
List<Provider> getAllProvider();
}
7.创建service.impl包
ProviderServiceImpl.java
package com.openlab.service.impl;
import com.openlab.dao.ProviderDao;
import com.openlab.pojo.Provider;
import com.openlab.service.ProviderService;
import java.util.List;
public class ProviderServiceImpl implements ProviderService {
private ProviderDao providerDao;
public ProviderDao getProviderDao() {
return providerDao;
}
public void setProviderDao(ProviderDao providerDao) {
this.providerDao = providerDao;
}
@Override
public List<Provider> getAllProvider() {
return providerDao.selectAllProvider();
}
}
8.配置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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/openlab/dao/**/*.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<bean id="providerDao" class="com.openlab.dao.impl.ProviderDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
<bean id="providerService" class="com.openlab.service.impl.ProviderServiceImpl">
<property name="providerDao" ref="providerDao"></property>
</bean>
</beans>
9.测试
package com.openlab.test;
import com.openlab.pojo.Provider;
import com.openlab.service.ProviderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test01 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ProviderService providerService = (ProviderService) ac.getBean("providerService");
List<Provider> allProvider = providerService.getAllProvider();
for(Provider provider : allProvider){
System.out.println(provider);
}
}
}
作业二
1.创建工程,导入相关的jar包
2.创建pojo包
Bill.java
package com.openlab.pojo;
import java.util.Date;
public class Bill {
private int id;
private String billCode;
private String productName;
private String productDesc;
private String productUnit;
private double productCount;
private double totalPrice;
private int isPayment;
private int createdBy;
private Date creationDate;
private int modifyBy;
private Date modifyDate;
private int providerId;
private String providerName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBillCode() {
return billCode;
}
public void setBillCode(String billCode) {
this.billCode = billCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductUnit() {
return productUnit;
}
public void setProductUnit(String productUnit) {
this.productUnit = productUnit;
}
public double getProductCount() {
return productCount;
}
public void setProductCount(double productCount) {
this.productCount = productCount;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public int getIsPayment() {
return isPayment;
}
public void setIsPayment(int isPayment) {
this.isPayment = isPayment;
}
public int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public int getProviderId() {
return providerId;
}
public void setProviderId(int providerId) {
this.providerId = providerId;
}
public String getProviderName() {
return providerName;
}
public void setProviderName(String providerName) {
this.providerName = providerName;
}
}
3.创建mybatis-config.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.openlab.pojo"></package>
</typeAliases>
</configuration>
4.创建dao包
BillDao.java
package com.openlab.dao;
import com.openlab.pojo.Bill;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BillDao {
List<Bill> selectBills(@Param("proName") String proName, @Param("providerId")String providerId,@Param("ifPay") Integer ifPay);
}
BillMapper.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.openlab.dao.BillDao">
<resultMap id="billResultMap" type="Bill">
<result property="providerName" column="pname"></result>
</resultMap>
<select id="selectBills" resultMap="billResultMap">
select b.*,p.proName as pname from smbms_bill b,smbms_provider p
<where>
b.providerId = p.id
<if test="proName!=null and proName!=''">
and b.productName like concat('%',#{proName},'%')
</if>
<if test="providerId!=null and providerId!=''">
and b.providerId = #{providerId}
</if>
<if test="ifPay!=null">
and b.isPayment = #{ifPay}
</if>
</where>
</select>
</mapper>
5.创建service包
BillService.java
package com.openlab.service;
import com.openlab.pojo.Bill;
import java.util.List;
public interface BillService {
List<Bill> getBills(String productName,String providerId,Integer ifPay);
}
6.创建service.impl包
BillServiceImpl.java
package com.openlab.service.impl;
import com.openlab.dao.BillDao;
import com.openlab.pojo.Bill;
import com.openlab.service.BillService;
import java.util.List;
public class BillServiceImpl implements BillService {
private BillDao billDao;
public BillDao getBillDao() {
return billDao;
}
public void setBillDao(BillDao billDao) {
this.billDao = billDao;
}
@Override
public List<Bill> getBills(String productName, String providerId, Integer ifPay) {
return billDao.selectBills(productName,providerId,ifPay);
}
}
7.配置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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/openlab/dao/**/*.xml"/>
</bean>
<bean id="billMapperProxy" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.openlab.dao.BillDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="billService" class="com.openlab.service.impl.BillServiceImpl">
<property name="billDao" ref="billMapperProxy"/>
</bean>
</beans>
8.测试
package com.openlab.test;
import com.openlab.pojo.Bill;
import com.openlab.service.BillService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test02 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BillService billService = (BillService) ac.getBean("billService");
List<Bill> bills = billService.getBills("", "2", 2);
for(Bill bill : bills){
System.out.println(bill.getBillCode() + "\t" + bill.getProductName() + "\t"
+ bill.getProviderName() + "\t" + bill.getTotalPrice()
+ "\t" + bill.getIsPayment() + "\t" + bill.getCreationDate());
}
}
}
作业三
1.修改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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/openlab/dao/**/*.xml"/>
</bean>
<context:component-scan base-package="com.openlab.dao,com.openlab.service"/>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.openlab.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
2.修改BillDao.java
package com.openlab.dao;
import com.openlab.pojo.Bill;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("billDao")
public interface BillDao {
List<Bill> selectBills(@Param("proName") String proName, @Param("providerId")String providerId,@Param("ifPay") Integer ifPay);
}
3.修改ProviderDao.java
package com.openlab.dao;
import com.openlab.pojo.Provider;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("providerDao")
public interface ProviderDao {
List<Provider> selectAllProvider();
}
4.修改BillServiceImpl.java
package com.openlab.service.impl;
import com.openlab.dao.BillDao;
import com.openlab.pojo.Bill;
import com.openlab.service.BillService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("billService")
public class BillServiceImpl implements BillService {
@Resource(name = "billDao")
private BillDao billDao;
public BillDao getBillDao() {
return billDao;
}
public void setBillDao(BillDao billDao) {
this.billDao = billDao;
}
@Override
public List<Bill> getBills(String productName, String providerId, Integer ifPay) {
return billDao.selectBills(productName,providerId,ifPay);
}
}
5.修改ProviderServiceImpl.java
package com.openlab.service.impl;
import com.openlab.dao.ProviderDao;
import com.openlab.pojo.Provider;
import com.openlab.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("providerService")
public class ProviderServiceImpl implements ProviderService {
@Autowired
@Qualifier("providerDao")
private ProviderDao providerDao;
public ProviderDao getProviderDao() {
return providerDao;
}
public void setProviderDao(ProviderDao providerDao) {
this.providerDao = providerDao;
}
@Override
public List<Provider> getAllProvider() {
return providerDao.selectAllProvider();
}
}
6.测试Test01.java 和 Test02.java
package com.openlab.test;
import com.openlab.pojo.Provider;
import com.openlab.service.ProviderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test01 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ProviderService providerService = (ProviderService) ac.getBean("providerService");
List<Provider> allProvider = providerService.getAllProvider();
for(Provider provider : allProvider){
System.out.println(provider);
}
}
}
package com.openlab.test;
import com.openlab.pojo.Bill;
import com.openlab.service.BillService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test02 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BillService billService = (BillService) ac.getBean("billService");
List<Bill> bills = billService.getBills("", "2", 2);
for(Bill bill : bills){
System.out.println(bill.getBillCode() + "\t" + bill.getProductName() + "\t"
+ bill.getProviderName() + "\t" + bill.getTotalPrice()
+ "\t" + bill.getIsPayment() + "\t" + bill.getCreationDate());
}
}
}