- 测试目录
- AppConfig
@ComponentScan("com.jxz.test")
public class AppConfig {
@Bean
public User user(){
User user = new User("测试@Bean", "ok");
return user;
}
}
- FruitDaoImpl
@Component("fruitDao")
public class FruitDaoImpl extends BaseDao<Fruit> {
@Override
protected Connection getConnection() {
return JDBCUtils.getConnection();
}
}
- Fruit User
package com.jxz.test.entity;
import com.jxz.annotation.TableField;
import com.jxz.annotation.TableName;
@TableName("fruit")
public class Fruit {
@TableField("fid")
private Integer fid;
@TableField("fname")
private String fname;
@TableField("price")
private Double price;
@TableField("count")
private Integer count;
@TableField("remark")
private String remark;
public Fruit(){}
public Fruit(Integer fid, String fname, Double price, Integer count, String remark) {
this.fid = fid;
this.fname = fname;
this.price = price;
this.count = count;
this.remark = remark;
}
public Integer getFid() {
return fid;
}
public void setFid(Integer fid) {
this.fid = fid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "Fruit{" +
"fid=" + fid +
", fname='" + fname + '\'' +
", price=" + price +
", count=" + count +
", remark='" + remark + '\'' +
'}';
}
}
public class User {
private String UserName;
private String password;
public User(){}
public User(String userName, String password) {
UserName = userName;
this.password = password;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"UserName='" + UserName + '\'' +
", password='" + password + '\'' +
'}';
}
}
- MyFilter
@WebFilter("/*")
public class MyFilter extends MainFilter {
@Override
protected Class getConfigClass() {
return AppConfig.class;
}
}
- FruitService
@Component("fruitService")
public class FruitService {
@Autowired
private FruitDaoImpl fruitDao;
public List<Fruit> getFruitList(){
List<Fruit> fruits = fruitDao.selectList();
return fruits;
}
public Fruit getFruitById(Integer id) {
return fruitDao.selectById(id);
}
}
- MyController
@Controller
@RequestMapping(url = "test")
public class MyController {
@Autowired
private FruitService fruitService;
@Autowired
User user;
@RequestMapping(url = "/a", method = MethodType.GET)
public List<Fruit> a(){
return fruitService.getFruitList();
}
@RequestMapping(url = "/b", method = MethodType.GET)
public Fruit b(@Param("id") Integer id){
return fruitService.getFruitById(id);
}
@RequestMapping(url = "/c", method = MethodType.GET)
public String c(HttpServletRequest request){
request.getSession().setAttribute("user", user);
return "redirect:/index.jsp";
}
}
- 测试
http://localhost:8080/test/a
http://localhost:8080/test/b?id=11
http://localhost:8080/test/c
如果有相同的@RequestMapping可能会有BUG 只是简单模拟了一下功能