代码结构如上
介绍逻辑结构:通过页面操作user,有注册,修改,删除,查找功能
DB脚本,超级简单
create table(
ID primary key not null auto_increase,
name varchar(50)
)
代码如下
创建封装类
public class User { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User(String name) { super(); this.name = name; } public User() { super(); } }
服务层 Interface public interface UserService { public void register(User user); public void update(User user); public User findById(int id); public void deleteById(int id); }
服务车实现类 public class UserServiceImpl implements UserService { //@Resource(name="jdbcUserDao") private UserDAO userDao; public void setUserDao(UserDAO userDao){ this.userDao = userDao; } @Override public void register(User user) { System.out.println("Service##用户注册处理"); userDao.save(user); } @Override public void update(User user) { System.out.println("Service##用户修改个人信息处理"); userDao.update(user); } @Override public User findById(int id) { return userDao.findById(id); } @Override public void deleteById(int id) { userDao.deleteById(id); } }
dao接口 public interface UserDAO { public void save(User user); public void update(User user); public void deleteById(int id); public User findById(int id); }
dao实现类 public class JDBCUserDAO extends JdbcDaoSupport implements UserDAO { @Override public void save(User user) { System.out.println("DAO##采用JDBC技术保存用户信息!"); String sql = "insert into t_user "+ "(name)"+"value(?)"; this.getJdbcTemplate().update(sql, new Object[]{ user.getName() }); } @Override public void update(User user) { System.out.println("DAO##采用JDBC技术更新用户信息!"); } @Override public void deleteById(int id) { String sql = "delete from t_user where id=?"; this.getJdbcTemplate() .update(sql,new Object[]{id}); } @Override public User findById(int id) { String sql="select * from t_user where id = ?"; User user = (User)this.getJdbcTemplate().queryForObject(sql, new Object[]{id}, new UserMapper()); User user1 = (User)this.getJdbcTemplate().queryForObject(sql, new Object[]{id}, new UserMapper()); return user; } }
spring javabean配置 <bean id="userService" class="mychebao.service.UserServiceImpl"> <property name="userDao" ref="jdbcUserDao"></property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="toor"></property> <property name="maxActive" value="10"></property> <property name="initialSize" value="2"></property> <property name="minIdle" value="2"></property> <property name="maxIdle" value="3"></property> </bean> <bean id="jdbcUserDao" class="mychebao.dao.JDBCUserDAO"> <property name="dataSource" ref="dataSource"></property> </bean>
注册web服务 @Path("/userInfoService") public class UserInfoService { @GET @Path("/get") @Produces(MediaType.APPLICATION_JSON) public String getUser(@QueryParam("id") String id){ ApplicationContext ac = new ClassPathXmlApplicationContext("schema.xml"); UserService userservice = (UserService)ac.getBean("userService"); User user = userservice.findById(Integer.parseInt(id)); return JSONObject.toJSONString(user); } @GET @Path("/add") @Produces(MediaType.APPLICATION_JSON) public String addUser(@QueryParam("name") String name){ ApplicationContext ac = new ClassPathXmlApplicationContext("schema.xml"); UserService userservice = (UserService)ac.getBean("userService"); //User user = userservice.findById(Integer.parseInt(id)); User user = new User(); user.setName(name); userservice.register(user); return "add successfully!"; //return JSONObject.toJSONString(user); } @GET @Path("/del") @Produces(MediaType.APPLICATION_JSON) public String delUser(@QueryParam("id") String id){ ApplicationContext ac = new ClassPathXmlApplicationContext("schema.xml"); UserService userservice = (UserService)ac.getBean("userService"); //User user = userservice.findById(Integer.parseInt(id)); userservice.deleteById(Integer.parseInt(id)); return "delete successfully!"; } }
4.看看结果
原始t_user表是空的;
a.添加一个用户
b.查看结果
c.页面查找指定user
d.页面删除一个user
e.查看DB
记录被清除了