SSM框架梳理(Spring+SpringMVC+MyBatis)
程序员文章站
2022-06-05 13:09:52
...
最近在维护一个javaweb项目的服务器代码,该项目采用的是mysql+mybatis+spring+springmvc+easyui,比较常用的模式;大概看了下架构,扩展性也比较强,把大致代码架构整理了一下,需要增加新功能时按架构添加代码即可。
代码示例:
1、databases增加表
22DROP TABLE IF EXISTS `customers`;
23/*!40101 SET @saved_cs_client = @@character_set_client */;
24/*!40101 SET character_set_client = utf8 */;
25CREATE TABLE `customers` (
26 `id` int(11) NOT NULL AUTO_INCREMENT,
27 `name` char(20) NOT NULL,
28 `address` char(50) DEFAULT NULL,
29 `city` char(50) DEFAULT NULL,
30 `age` int(11) NOT NULL,
31 `love` char(50) NOT NULL DEFAULT 'No habbit',
32 PRIMARY KEY (`id`)
33) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
34/*!40101 SET character_set_client = @saved_cs_client */;
35
36--
37-- Dumping data for table `customers`
38--
39
40LOCK TABLES `customers` WRITE;
41/*!40000 ALTER TABLE `customers` DISABLE KEYS */;
42INSERT INTO `customers` VALUES (1,'liu','chongqin','yuzhongque',28,'ooxx');
43/*!40000 ALTER TABLE `customers` ENABLE KEYS */;
44UNLOCK TABLES;
2.添加mybatis映射表
<?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.agesets.dao.ICustomersDAO">
<resultMap type="com.agesets.model.Acustomers" id="customersMap">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="address" property="address" />
<result column="city" property="city" />
<result column="age" property="age" />
<result column="love" property="love" />
</resultMap>
<sql id="cloumn">
a.id, a.name, a.address, a.city, a.age, a.love
</sql>
<select id="selectCustomers" parameterType="com.agesets.model.Acustomers" resultMap="customersMap">
select * from customers a where a.id = 1
</select>
</mapper>
—-mybatis/config.xml
<mapper resource="mybatis/AAcustomers.xml" />
3.entity
package com.agesets.controller;
import java.beans.PropertyEditorSupport;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.agesets.model.About;
import com.agesets.model.Acustomers;
import com.agesets.model.Version;
import com.agesets.service.AboutService;
import com.agesets.service.AcustomersService;
import com.agesets.service.VersionService;
@Controller
@RequestMapping(value="/customers")
public class AcustomersController {
private AcustomersService acustomersService;
@Autowired
public void setAcustomersService(AcustomersService acustomersService) {
this.acustomersService = acustomersService;
}
@RequestMapping
public String list(Model model) {
Acustomers acustomers = acustomersService.selectCustomers();
model.addAttribute("acustomers", acustomers);
return "jsp/about/about";
}
}
4.DAO
package com.agesets.dao;
import java.util.List;
import com.agesets.model.Acustomers;
public interface ICustomersDAO {
Acustomers selectCustomers();
}
5.service
—-service
package com.agesets.service;
import java.util.List;
import com.agesets.model.About;
import com.agesets.model.Acustomers;
public interface AcustomersService {
Acustomers selectCustomers();
}
—-serviceimpl
package com.agesets.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.agesets.dao.ICustomersDAO;
import com.agesets.model.Acustomers;
import com.agesets.service.AcustomersService;
@Service("AcustomersService")
public class AcustomersServiceImpl implements AcustomersService {
private ICustomersDAO customersDao;
@Autowired
public void setcustomersDao(ICustomersDAO customersDao) {
this.customersDao = customersDao;
}
@Override
public Acustomers selectCustomers() {
return customersDao.selectCustomers();
}
}
6.controller
package com.agesets.controller;
import java.beans.PropertyEditorSupport;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.agesets.model.Acustomers;
import com.agesets.service.AcustomersService;
@Controller
@RequestMapping(value="/customers")
public class AcustomersController {
private AcustomersService acustomersService;
@Autowired
public void setAcustomersService(AcustomersService acustomersService) {
this.acustomersService = acustomersService;
}
@RequestMapping
public String list(Model model) {
Acustomers acustomers = acustomersService.selectCustomers();
model.addAttribute("acustomers", acustomers);
return "jsp/about/about";
}
}
7.jsp
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="main_table">
<tr class="main_head">
<th>id</th>
<th>姓名</th>
<th>地址</th>
<th>城市</th>
<th>年龄</th>
<th>爱好</th>
</tr>
<tr class="main_info">
<td>${acustomers.id}</td>
<td>${acustomers.name}</td>
<td>${acustomers.address}</td>
<td>${acustomers.city}</td>
<td>${acustomers.age}</td>
<td>${acustomers.love}</td>
</tr>
</table>
end