在UserService.java中写:
package com.yhp.service;
import java.util.List;
import com.yhp.model.User;
public interface UserService {
public List list(User user);
1
}
在UserServiceImpl.java中写:
package com.yhp.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yhp.dao.UserDao;
import com.yhp.model.User;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List list(User user) {
return userDao.list(user);
}
1
2
3
4
5
6
7
}
在User.java中写:
package com.yhp.model;
public class User {
private Integer id;
private String username;
private String pwd;
private String realname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
}
UserController.java中写:
package com.yhp.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yhp.model.User;
import com.yhp.service.UserService;
@RequestMapping("/user")
@Controller
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("/list.do")
public List list(User user){
return userService.list(user);
}
@RequestMapping("/toLogin.do")
public String toLogin(){
return “login”;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
}
另需配置xml文件springmvc-config.xml:
<context:component-scan base-package=“com.yhp.controller” />
<mvc:annotation-driven />
<mvc:resources location="/js/" mapping="/js/" />
<mvc:resources location="/css/" mapping="/css/" />
<mvc:resources location="/images/" mapping="/images/**" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
和mybatis-config.xml文件: <?xml version="1.0" encoding="UTF-8" ?> 还有applicationContext.xml文件:
<context:property-placeholder location=“classpath:jdbc.properties” />
<aop:aspectj-autoproxy />
<tx:annotation-driven transaction-manager=“transactionManager” />