springMVC3+JDBCTemplate简单示例
看了几天的spring资料,查看了很多网上资料(包括了官网的例子),发现很少有关springMVC和jdbcTemplate结合使用的完整而且简单的例子,今天晚上很有空就自己写了简单的例子,自己也是刚刚学springMVC,所以希望能为像我这样的菜鸟能快速入门,希望大家在看完我写的教程的时候,也能留言,说点建议,这是我第一次写例子,希望大家多多支持
开发环境:
Springsource tool suite
Spring 的包
commons-dbcp.jar
commons-logging-1.0.4.jar
commons-pool.jar
mysql-connector-java-3.1.14-bin.jar
step1:建立数据库
如图
step2:使用springsource toolsuite 新建一个mvc工程,我的工程名字为:springMVC_demo
删除原有的homecontroller
step3:新建user实体
[java]
package com.xing.ent;
/**
* guangzhoudaxue
* @author xing
*
*/
public class User{
private int id;
private String name;
private String password;
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ "]";
}
}
Step4:新建UserDao
[java]
package com.xing.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import com.xing.ent.User;
/**
* guangzhoudaxue
* @author xing
*
*/
public class UserDao {
class UserRowMapper implements RowMapper{
public Object mapRow(ResultSet rs,int index) throws SQLException {
User u = new User(rs.getString("name"), rs.getString("password"));
return u;
}
}
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
//注入
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public List findAllUser()
{
List list = null;
try{
String sql ="select * from user";
list = jdbcTemplate.query(sql,new RowMapperResultSetExtractor(new UserRowMapper()));
}catch (Exception e) {
// TODO: handle exception
}
return list;
}
}
Step5新建UserService
[java]
package com.xing.service;
import java.util.List;
import com.xing.dao.UserDao;
import com.xing.ent.User;
/**
* guangzhoudaxue
* @author xing
*
*/
public class UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
//注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/**
* 查找所有的用户信息
* @return
*/
public List<User> getUserList(){
return userDao.findAllUser();
}
}
Step6:新建UserController
[java]
package com.xing.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xing.ent.User;
import com.xing.service.UserService;
/**
* guangzhoudaxue
* @author xing
*
*/
@Controller
public class UserCon
补充:软件开发 , Java ,