创建项目
导入jar包
编写User类
User.java
package cn.lexed.pojo;
public class User {
private String username;
private Integer age;
public User() {
super();
}
public User(String username, Integer age) {
super();
this.username = username;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [username=" + username + ", age=" + age + "]";
}
}
编写UserService和UserSeviceImpl
UserService.java服务器托管网
package cn.lexed.service;
import cn.lexed.pojo.User;
public interface UserService {
void addUser(User u);
}
UserServiceImpl.java
package cn.lexed.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.lexed.dao.UserDao;
import cn.lexed.pojo.User;
import cn.lexed.service.UserService;
//
@Service("userService")
服务器托管网public class UserServiceImpl implements UserService{
//在service的实现类中,定义一个Dao层接口类型的属性
@Resource(name="userDao")
private UserDao udao;//uDao本质上就是Dao层实现类的对象
@Override
public void addUser(User u) {
// TODO Auto-generated method stub
udao.addUser(u);
}
}
编写UserDao和UserDaoImpl
UserDao.java
package cn.lexed.dao;
import cn.lexed.pojo.User;
public interface UserDao {
void addUser(User u);
void updateUser(User u);
void deleteUser(int id);
}
UserDaoImpl.java
package cn.lexed.dao.impl;
//面向对象的编程OOP
//面向接口的编程IOP
//面向切面的编程AOP
import org.springframework.stereotype.Repository;
import cn.lexed.dao.UserDao;
import cn.lexed.pojo.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao{
@Override
public void addUser(User u) {
// TODO Auto-generated method stub
System.out.println("user添加成功");
}
@Override
public void updateUser(User u) {
// TODO Auto-generated method stub
}
@Override
public void deleteUser(int id) {
// TODO Auto-generated method stub
}
}
编写配置文件(点击Namespaces添加context)
app.xml
编写测试类
Test.java
package cn.lexed.test;
import static org.junit.Assert.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.lexed.pojo.User;
import cn.lexed.service.UserService;
public class Test {
@org.junit.Test
public void test() {
//1.创建Spring容器的对象
ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
UserService ser=(UserService)ac.getBean("userService");
User u=new User("xxx",12);
ser.addUser(u);
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
概述 泛型是一种将类型参数化的动态机制,使用得到的话,可以从以下的方面提升的你的程序: 安全性:使用泛型可以使代码更加安全可靠,因为泛型提供了编译时的类型检查,使得编译器能够在编译阶段捕捉到类型错误。通过在编译时检查类型一致性,可以避免在运行时出现类型转换错误…