第一步在新建一个工程名为WebProject ,在该工程的src/main/java的com.xcy.ctrl下新建一个Controller(familyController)
在这一步中@Controller是必不可少的,每个RequestMapping都要返回一个界面,这里暂时假定为true.jsp界面
@Controller
@RequestMapping("/family")
public class FamilyController {
ModelAndView mv = new ModelAndView();//初始化
System.out.println("==========");
mv.setViewName("true");//设定访问见面为true.jsp
return mv;//返回界面
}
第二步在com.xcy.bean下新建一个家庭记账信息的基本属性FamilyInfo.java
package com.xcy.bean;
import java.util.Date;
public class FamilyInfo {
int fid;
Date day;
String classification;
float price;
String remark;
String dayStr;
public String getDayStr() {
return dayStr;
}
public void setDayStr(String dayStr) {
this.dayStr = dayStr;
}
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
public Date getDay() {
return day;
}
public void setDay(Date day) {
this.day = day;
}
public String getClassification() {
return classification;
}
public void setClassification(String classification) {
this.classification = classification;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
第三步进行增加记账信息记录
(1)在familyController中增加一个@RequestMapping
@Controller
@RequestMapping("/family")
public class FamilyController {
@Autowired
public FamilyInfoService cost;
@RequestMapping("/add")
public ModelAndView index(HttpServletRequest req, HttpServletResponse res){
String day=req.getParameter("day");//从浏览器中获得日期值,这个day必须和familyInfo里的属性名一致,下同
SimpleDateFormat formatter= new SimpleDateFormat ("yyyy-MM-dd");//将日期格式设置成"yyyy-MM-dd"格式
Date d = null;
try {
d=formatter.parse(day);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String classification=req.getParameter("classification");//从浏览器中获得类别值
String price=req.getParameter("price");//从浏览器中获得价格值,注意从浏览器获得的数据都是String型
Float p=Float.parseFloat(price);//将String型转换成Float型
String remark=req.getParameter("remark");//从浏览器中获得备注值
FamilyInfo c=new FamilyInfo();//新建一个记账信息c
c.setDay(d);//设置Day
c.setClassification(classification);//设置classification
c.setPrice(p);//设置Price
c.setRemark(remark);//设置remark
cost.add(c);// 增加记账信息
ModelAndView mv = new ModelAndView();//初始化
System.out.println("==========");
mv.setViewName("true");//设定访问见面为hello.jsp
return mv;//返回界面
}
(2)在com.xcy.service中新 建一个Service接口(FamilyInfoService),在该Service中新建一个方法
package com.xcy.service;
import java.util.Date;
import java.util.List;
import com.xcy.bean.FamilyInfo;
public interface FamilyInfoService {
public int add(FamilyInfo c);
}
(3)在com.xcy.service.xml中新 建一个ServiceImpl(FamilyInfoServiceImpl),在该Service中新建一个方法
package com.xcy.service.impl;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xcy.bean.FamilyInfo;
import com.xcy.mapper.FamilyInfoMapper;
import com.xcy.service.FamilyInfoService;
@Service
public class FamilyServiceImpl implements FamilyInfoService{
@Autowired
public FamilyInfoMapper family;
@Override
public int add(FamilyInfo c) {
// TODO Auto-generated method stub
System.out.println("addfamilycost");
family.add(c);
System.out.println("addfamilycost success");
return 0;
}
}
**在public class FamilyServiceImpl implements FamilyInfoService之前要有@Service进行注解,记住接口是用来被继承的 implements+接口名
(4)在com.xcy.mapper中新 建一个mapper(FamilyInfoMapper),它也是一个接口
package com.xcy.mapper;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.xcy.bean.FamilyInfo;
public interface FamilyInfoMapper {
public int add(FamilyInfo c);
}
(5)在spring.mapping中新增一个.xml文件(Family.xml),并在该文件中编辑相应的sql语句
<?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.xcy.mapper.FamilyInfoMapper">
<insert id="add" parameterType="com.xcy.bean.FamilyInfo"><![CDATA[
INSERT INTO family (day,classification,price,remark)
VALUES (#{day}, #{classification}, #{price}, #{remark})
]]></insert>
</mapper>
之后要将Product.xml文件引入到mybatisConfig.xml文件中
<mapper resource=“spring/mapping/Family.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>
<settings>
<setting name="jdbcTypeForNull" value="NULL"/>
</settings>
<plugins>
<plugin interceptor="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
<property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.OracleDialect"/>
</plugin>
</plugins>
<mappers>
<mapper resource="spring/mapping/Users.xml"/>
<mapper resource="spring/mapping/Cost.xml"/>
<mapper resource="spring/mapping/Student.xml"/>
<mapper resource="spring/mapping/Category.xml"/>
<mapper resource=“spring/mapping/Product.xml"/>
<mapper resource="spring/mapping/Address.xml"/>
<mapper resource="spring/mapping/Report.xml"/>
<mapper resource="spring/mapping/Family.xml"/>
</mappers>
</configuration>
第四步进行查询记账信息记录
(1)在familyController中增加一个@RequestMapping(“/query"),这是查询所有数据库中的记账信息
@RequestMapping("/query")
public ModelAndView query(HttpServletRequest req, HttpServletResponse res){
ModelAndView mv= new ModelAndView();//mv界面初始化
System.out.println("==========");
List<FamilyInfo> list = cost.getAllUser();//获取所有消费信息列表
for(int i = 0; i< list.size() ;i++){
FamilyInfo c = list.get(i);
System.out.println("===="+ "day:" + c.getDay() + "classification:"+ c.getClassification()
+ "price:" + c.getPrice()+"remark:"+c.getRemark());
}//输出所有信息
mv.addObject("list", list);//对应到.jsp,这是通过页面将数据传到前台
mv.setViewName("form2-query");//返回form2-query.jsp界面
return mv;
}
(2)在FamilyInfoService中增加一个查询方法
public List<FamilyInfo> getAllUser();
(3)在FamilyInfoServiceImpl中增加一个查询实现的方法
@Override
public List<FamilyInfo> getAllUser() {
// TODO Auto-generated method stub
return family.queryAll();
}
(4)在FamilyInfoMapper中增加一个查询方法
public List<FamilyInfo> queryAll();
(5)在Family.xml文件中编辑相应的sql语句
<select id="queryAll" parameterType="string" resultType="com.xcy.bean.FamilyInfo">
select * from family
</select>
第五步若要进行按条件查询记账信息记录则进行下面的方法
(1)在familyController中增加一个@RequestMapping("/queryByDay"),这是按日期查询所有数据库中的记账信息
@RequestMapping("/queryByDay")
public ModelAndView queryFamilyInfo(HttpServletRequest req, HttpServletResponse res){
String day=req.getParameter("day");
SimpleDateFormat formatter= new SimpleDateFormat ("yyyy-MM-dd");
Date d = null;
try {
d=formatter.parse(day);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<FamilyInfo> queue =cost.queryByDay(d);
for(int i = 0; i< queue.size() ;i++){
FamilyInfo c = queue.get(i);
System.out.println("===="+ "day:" + c.getDay() + "classification:"+ c.getClassification()+ "price:" + c.getPrice()+"remark:"+c.getRemark());
}
ModelAndView mv= new ModelAndView();
System.out.println("==========");
mv.addObject("list", queue);
mv.setViewName("form2-query");
(2)在FamilyInfoService中增加一个查询方法
public List<FamilyInfo> queryByDay(Date d);
(3)在FamilyInfoServiceImpl中增加一个查询实现的方法
@Override
public List<FamilyInfo> queryByDay(Date d) {
// TODO Auto-generated method stub
return family.queryByDay(d);
}
(4)在FamilyInfoMapper中增加一个查询方法
public List<FamilyInfo> queryByDay(Date d);
(5)在Family.xml文件中编辑相应的sql语句
<select id="queryByDay" parameterType="string" resultType="com.xcy.bean.FamilyInfo">
select * from family
<if test="day != null">
where day = #{day}
</if>
</select>
第六步进行更新记账信息记录
(1)在familyController中增加一个@RequestMapping(“/update")
@RequestMapping("/update")
public ModelAndView updateFamilyInfo(HttpServletRequest req, HttpServletResponse res){
String id1= req.getParameter("id");
int id=Integer.parseInt(id1);
String day=req.getParameter("day");
SimpleDateFormat formatter= new SimpleDateFormat ("yyyy-MM-dd");
Date d = null;
try {
d=formatter.parse(day);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String classification=req.getParameter("classification");
String price=req.getParameter("price");
Float p=Float.parseFloat(price);
String remark=req.getParameter("remark");
FamilyInfo c=new FamilyInfo();
c.setFid(id);
c.setDay(d);
c.setClassification(classification);
c.setPrice(p);
c.setRemark(remark);
cost.updateFamily(c);
ModelAndView mv= new ModelAndView();
System.out.println("==========");
mv.setViewName("success");
return mv;
}
(2)在FamilyInfoService中增加一个更新方法
public void updateFamily(FamilyInfo c);
(3)在FamilyInfoServiceImpl中增加一个更新实现的方法
@Override
public void updateFamily(FamilyInfo c) {
// TODO Auto-generated method stub
family.update(c);
}
(4)在FamilyInfoMapper中增加一个更新方法
public void update(FamilyInfo c);
(5)在Family.xml文件中编辑相应的sql语句
<update id="update" parameterType="com.xcy.bean.FamilyInfo">
update family
<trim prefix="set" suffixOverrides=",">
<if test="day != null">
day = #{day},
</if>
<if test="classification != null">
classification = #{classification},
</if>
<if test="price != null">
price = #{price},
</if>
<if test="remark != null">
remark = #{remark},
</if>
</trim>
<trim prefix="where" prefixOverrides="and">
<if test="fid != null">
and id = #{fid}
</if>
</trim>
</update>
第七步删除记账信息记录
(1)在familyController中增加一个@RequestMapping("/deleteById")
@RequestMapping("/deleteById")
public ModelAndView deleteFamilyInfo(HttpServletRequest req, HttpServletResponse res){
String id1= req.getParameter("fid");
int fid=Integer.parseInt(id1);
cost.deleteFamily(fid);
ModelAndView mv= new ModelAndView();
System.out.println("==========");
mv.setViewName("form2-update");
return mv;
}
(2)在FamilyInfoService中增加一个删除方法
public void deleteFamily(int fid);
(3)在FamilyInfoServiceImpl中增加一个删除实现的方法
@Override
public void deleteFamily(int fid) {
// TODO Auto-generated method stub
family.deleteById(fid);
}
(4)在FamilyInfoMapper中增加一个删除方法
public void deleteById(int fid);
(5)在Family.xml文件中编辑相应的sql语句
<delete id="deleteById" parameterType="com.xcy.bean.FamilyInfo">
delete from family where fid = #{fid}
</delete>