第一周项目功能实现

一、本周完成的功能:

  a.客户信息管理

  b.车辆信息管理

  c.驾驶员信息管理

  d.业务员信息管理

二、核心源码:

1、配置文件springmvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<context:component-scan base-package="com.hp" />
<mvc:annotation-driven />

<!-- 配置数据库连接池 (c3p0) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本信息 -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/vdms?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=Asia/Shanghai" />
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="user" value="root" />
<property name="password" value="root" />
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="5" />
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="5" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="15" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="20" />
<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="15" />
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800" />
</bean>

<!-- mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis配置文件
<property name="configLocation" value="classpath:config/mybatis-config.xml"></property> -->
<!-- mapper文件放的地址 -->
<property name="mapperLocations" value="classpath:com/hp/mapper/*.xml"></property>
</bean>

<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property
name="sqlSessionFactory" ref="sqlSessionFactory"></property> <property name="basePackage"
value="com.hp.dao"></property> </bean> -->

<!-- 配置sqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

<!-- 视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".html"></property>
</bean> -->

<!-- 静态资源处理
<mvc:default-servlet-handler/>
<mvc:resources location="/view/" mapping="/view/**"/> -->
<mvc:resources location="/" mapping="/**" />

<!-- 开启注解事务 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>

2、通用dao层BaseDao编写

package com.hp.dao;

public interface BaseDao {

public Object insert(String sql, Object obj);
public Object update(String sql, Object obj);
public Object delete(String sql, Object obj);
public Object findForObject(String sql, Object obj);
public Object findForList(String sql, Object obj);
public Object findForMap(String sql, Object obj, String key, String value);
}

3、通用dao实现类BaseDaoImpl编写

package com.hp.dao.impl;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hp.dao.BaseDao;

@Repository("baseDao")
public class BaseDaoImpl implements BaseDao{

@Autowired
private SqlSessionTemplate sqlSessionTemplate;

@Override
public Object insert(String sql, Object obj) {
return sqlSessionTemplate.insert(sql, obj);
}

@Override
public Object update(String sql, Object obj) {
return sqlSessionTemplate.update(sql, obj);
}

@Override
public Object delete(String sql, Object obj) {
return sqlSessionTemplate.delete(sql,obj);
}

@Override
public Object findForObject(String sql, Object obj) {
return sqlSessionTemplate.selectOne(sql, obj);
}

@Override
public Object findForList(String sql, Object obj) {
return sqlSessionTemplate.selectList(sql, obj);
}

@Override
public Object findForMap(String sql, Object obj, String key, String value) {
return sqlSessionTemplate.selectMap(sql, obj,key);
}

}

4、service接口编写

public interface CustomerService {
public List<Customer> findAll();
public Integer addCustomer(Customer customer);
public Customer findById(Integer cstId);
public Integer updCustomer(Customer customer);
public Integer delCustomer(Integer cstId);
}

public interface CarinfoService {
public List<Carinfo> findAll();
public Integer addCarinfo(Carinfo carinfo);
public Carinfo findById(Integer cstId);
public Integer updCarinfo(Carinfo carinfo);
public Integer delCarinfo(Integer cstId);
}

public interface DriverService {
public List<Driver> findAll();
public Integer addDriver(Driver driver);
public Driver findById(Integer driId);
public Integer updDriver(Driver driver);
public Integer delDriver(Integer driId);
}

public interface SalesmanService {
public List<Salesman> findAll();
public Integer addSales(Salesman salesman);
public Salesman findById(Integer salId);
public Integer updSales(Salesman salesman);
public Integer delSales(Integer salId);
}

5、service层接口实现类

@Service("customerService")
public class CustomerServiceImpl implements CustomerService{

@Autowired
private BaseDao baseDao;

@Override
public List<Customer> findAll() {
return (List<Customer>) baseDao.findForList("CustomerMapper.findAllCustomer", null);
}

@Override
public Integer addCustomer(Customer customer) {
return (Integer) baseDao.insert("CustomerMapper.addCustomer", customer);
}

@Override
public Customer findById(Integer cstId) {
return (Customer) baseDao.findForObject("CustomerMapper.findCustById", cstId);
}

@Override
public Integer updCustomer(Customer customer) {
return (Integer) baseDao.insert("CustomerMapper.updCustomer", customer);
}

@Override
public Integer delCustomer(Integer cstId) {
return (Integer) baseDao.insert("CustomerMapper.delCustomer", cstId);
}

}

@Service("carinfoService")
public class CarinfoServiceImpl implements CarinfoService{

@Autowired
private BaseDao baseDao;

@Override
public List<Carinfo> findAll() {
return (List<Carinfo>) baseDao.findForList("CarinfoMapper.findAllCar", null);
}

@Override
public Integer addCarinfo(Carinfo carinfo) {
return (Integer) baseDao.insert("CarinfoMapper.addCar", carinfo);
}

@Override
public Carinfo findById(Integer cstId) {
return (Carinfo) baseDao.findForObject("CarinfoMapper.findCarById", cstId);
}

@Override
public Integer updCarinfo(Carinfo carinfo) {
return (Integer) baseDao.insert("CarinfoMapper.updCar", carinfo);
}

@Override
public Integer delCarinfo(Integer cstId) {
return (Integer) baseDao.insert("CarinfoMapper.delCar", cstId);
}
}

@Service("driverService")
public class DriverServiceImpl implements DriverService{

@Autowired
private BaseDao baseDao;

@Override
public List<Driver> findAll() {
return (List<Driver>) baseDao.findForList("DriverMapper.findAllDriver", null);
}

@Override
public Integer addDriver(Driver driver) {
return (Integer) baseDao.insert("DriverMapper.addDriver", driver);
}

@Override
public Driver findById(Integer driId) {
return (Driver) baseDao.findForObject("DriverMapper.findDriverById", driId);
}

@Override
public Integer updDriver(Driver driver) {
return (Integer) baseDao.insert("DriverMapper.updDriver", driver);
}

@Override
public Integer delDriver(Integer driId) {
return (Integer) baseDao.insert("DriverMapper.delDriver", driId);
}

}

@Service("salesmanService")
public class SalesmanServiceImpl implements SalesmanService{

@Autowired
private BaseDao baseDao;

@Override
public List<Salesman> findAll() {
return (List<Salesman>) baseDao.findForList("SalesmanMapper.findAllSales", null);
}

@Override
public Integer addSales(Salesman salesman) {
return (Integer) baseDao.insert("SalesmanMapper.addSales", salesman);
}

@Override
public Salesman findById(Integer salId) {
return (Salesman) baseDao.findForObject("SalesmanMapper.findSalesById", salId);
}

@Override
public Integer updSales(Salesman salesman) {
return (Integer) baseDao.insert("SalesmanMapper.updSales", salesman);
}

@Override
public Integer delSales(Integer salId) {
return (Integer) baseDao.insert("SalesmanMapper.delSales", salId);
}
}

6、Mapper配置文件

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="CustomerMapper">
<select id="findAllCustomer" resultType="com.hp.bean.Customer">
select * from customer
</select>
<insert id="addCustomer" parameterType="com.hp.bean.Customer">
insert into customer(cstName,cstContact,cstPhone,cstAddr,cstWechat,cstEmail)
values(#{cstName},#{cstContact},#{cstPhone},#{cstAddr},#{cstWechat},#{cstEmail})
</insert>
<select id="findCustById" resultType="com.hp.bean.Customer">
select * from customer where cstId = #{cstId}
</select>
<update id="updCustomer" parameterType="com.hp.bean.Customer">
update customer set cstName = #{cstName}, cstContact = #{cstContact}, cstPhone = #{cstPhone},
cstAddr = #{cstAddr}, cstWechat = #{cstWechat}, cstEmail = #{cstEmail}
where cstId = #{cstId}
</update>
<delete id="delCustomer">
delete from customer where cstId = #{cstId}
</delete>
</mapper>>

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="CarinfoMapper">
<select id="findAllCar" resultType="com.hp.bean.Carinfo">
select * from carinfo
</select>
<insert id="addCar" parameterType="com.hp.bean.Carinfo">
insert into carinfo(carNum,seatNum,carModel,frameNum)
values(#{carNum},#{seatNum},#{carModel},#{frameNum})
</insert>
<select id="findCarById" resultType="com.hp.bean.Carinfo">
select * from carinfo where carId = #{carId}
</select>
<update id="updCar" parameterType="com.hp.bean.Carinfo">
update carinfo set carNum = #{carNum}, seatNum = #{seatNum}, carModel = #{carModel},
frameNum = #{frameNum} where carId = #{carId}
</update>
<delete id="delCar">
delete from carinfo where carId = #{carId}
</delete>
</mapper>

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="DriverMapper">
<select id="findAllDriver" resultType="com.hp.bean.Driver">
select * from driver
</select>
<insert id="addDriver" parameterType="com.hp.bean.Driver">
insert into driver(driName,driIpcard,driPhone,driAddr,driNumber,driValidity,workValidaty)
values(#{driName},#{driIpcard},#{driPhone},#{driAddr},#{driNumber},#{driValidity},#{workValidaty})
</insert>
<select id="findDriverById" resultType="com.hp.bean.Driver">
select * from driver where driId = #{driId}
</select>
<update id="updDriver" parameterType="com.hp.bean.Driver">
update driver set driName = #{driName}, driIpcard = #{driIpcard}, driPhone = #{driPhone},
driAddr = #{driAddr}, driNumber = #{driNumber}, driValidity = #{driValidity}, workValidaty = #{workValidaty}
where driId = #{driId}
</update>
<delete id="delDriver">
delete from driver where driId = #{driId}
</delete>
</mapper>

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="SalesmanMapper">
<select id="findAllSales" resultType="com.hp.bean.Salesman">
select * from salesman
</select>
<insert id="addSales" parameterType="com.hp.bean.Salesman">
insert into salesman(salName,salIpcard,salJob,salPhone)
values(#{salName},#{salIpcard},#{salJob},#{salPhone})
</insert>
<select id="findSalesById" resultType="com.hp.bean.Salesman">
select * from salesman where salId = #{salId}
</select>
<update id="updSales" parameterType="com.hp.bean.Salesman">
update salesman set salName = #{salName}, salIpcard = #{salIpcard}, salJob = #{salJob},
salPhone = #{salPhone} where salId = #{salId}
</update>
<delete id="delSales">
delete from salesman where salId = #{salId}
</delete>
</mapper>

7、controller层编写

@Controller
@Scope("prototype")
public class CustomerController {

@Autowired
private CustomerService customerService;

@RequestMapping(value = "/finAllCust",produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
@ResponseBody
public String findAll() {
List<Customer> list = customerService.findAll();
return JSONArray.fromObject(list).toString();
}

@RequestMapping(value = "/toaddcust",method = RequestMethod.POST)
@ResponseBody
public String toadd() {
return "addcust.html";
}

@RequestMapping(value = "/addCust",method = RequestMethod.POST)
@ResponseBody
public String addCust(Customer customer) {
if(null == customer.getCstId()) {
customerService.addCustomer(customer);
}else {
customerService.updCustomer(customer);
}
return "customer.html";
}

@RequestMapping(value="/editCust",method = RequestMethod.POST)
@ResponseBody
public String editCust(Integer cstId) {
return "addcust.html?cstId="+cstId;
}

@RequestMapping(value = "/findCustById",produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String findById(Integer cstId) {
Customer customer = customerService.findById(cstId);
return JSONObject.fromObject(customer).toString();
}

@RequestMapping(value="/delCust",method = RequestMethod.POST)
@ResponseBody
public String delCust(Integer cstId) {
customerService.delCustomer(cstId);
return "customer.html";
}
}

@Controller
@Scope("prototype")
public class CarinfoController {

@Autowired
private CarinfoService carinfroService;

@RequestMapping(value = "/finAllCar",produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
@ResponseBody
public String findAll() {
List<Carinfo> list = carinfroService.findAll();
return JSONArray.fromObject(list).toString();
}

@RequestMapping(value = "/toaddcar",method = RequestMethod.POST)
@ResponseBody
public String toadd() {
return "addcar.html";
}

@RequestMapping(value = "/addCar",method = RequestMethod.POST)
@ResponseBody
public String addCar(Carinfo carinfo) {
if(null == carinfo.getCarId()) {
carinfroService.addCarinfo(carinfo);
}else {
carinfroService.updCarinfo(carinfo);
}
return "carinfo.html";
}

@RequestMapping(value="/editCar",method = RequestMethod.POST)
@ResponseBody
public String editCar(Integer carId) {
return "addcar.html?carId="+carId;
}

@RequestMapping(value = "/findCarById",produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String findById(Integer carId) {
Carinfo carinfo = carinfroService.findById(carId);
return JSONObject.fromObject(carinfo).toString();
}

@RequestMapping(value="/delCar",method = RequestMethod.POST)
@ResponseBody
public String delCar(Integer carId) {
carinfroService.delCarinfo(carId);
return "carinfo.html";
}
}

 

@Controller
@Scope("prototype")
public class DriverController {

@Autowired
private DriverService driverService;

@RequestMapping(value = "/finAllDriver",produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
@ResponseBody
public String findAll() {
List<Driver> list = driverService.findAll();
return JSONArray.fromObject(list).toString();
}

@RequestMapping(value = "/toadddriver",method = RequestMethod.POST)
@ResponseBody
public String toadd() {
return "adddriver.html";
}

@RequestMapping(value = "/addDriver",method = RequestMethod.POST)
@ResponseBody
public String addDriver(Driver driver) {
if(null == driver.getDriId()) {
driverService.addDriver(driver);
}else {
driverService.updDriver(driver);
}
return "driver.html";
}

@RequestMapping(value="/editDriver",method = RequestMethod.POST)
@ResponseBody
public String editDriver(Integer driId) {
return "adddriver.html?driId="+driId;
}

@RequestMapping(value = "/findDriverById",produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String findById(Integer driId) {
Driver driver = driverService.findById(driId);
return JSONObject.fromObject(driver).toString();
}

@RequestMapping(value="/delDriver",method = RequestMethod.POST)
@ResponseBody
public String delDriver(Integer driId) {
driverService.delDriver(driId);
return "driver.html";
}
}

@Controller
@Scope("prototype")
public class SalesmanController {

@Autowired
private SalesmanService salesmanService;

@RequestMapping(value = "/finAllSales",produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
@ResponseBody
public String findAll() {
List<Salesman> list = salesmanService.findAll();
return JSONArray.fromObject(list).toString();
}

@RequestMapping(value = "/toaddsales",method = RequestMethod.POST)
@ResponseBody
public String toadd() {
return "addsales.html";
}

@RequestMapping(value = "/addSales",method = RequestMethod.POST)
@ResponseBody
public String addSales(Salesman salesman) {
if(null == salesman.getSalId()) {
salesmanService.addSales(salesman);
}else {
salesmanService.updSales(salesman);
}
return "salesman.html";
}

@RequestMapping(value="/editSales",method = RequestMethod.POST)
@ResponseBody
public String editSales(Integer salId) {
return "addsales.html?carId="+salId;
}

@RequestMapping(value = "/findSaelsById",produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String findById(Integer salId) {
Salesman salesman = salesmanService.findById(salId);
return JSONObject.fromObject(salesman).toString();
}

@RequestMapping(value="/delSales",method = RequestMethod.POST)
@ResponseBody
public String delSales(Integer salId) {
salesmanService.delSales(salId);
return "salesman.html";
}
}

8、部分效果图

客户信息列表

 

 添加客户

 

 修改客户

 

 删除功能效果图、

 

 其他效果图和上面效果图类似,这里不再一一截图了

三、遇到的问题:

  暂无

四、解决办法

  暂无

五、燃尽图

 

posted @ 2020-07-22 14:59  金哥2713  阅读(172)  评论(0)    收藏  举报