具体讲解看xml文件//pojo 基本类
//DAO类
//服务类
//控制类1、写基本类pojo
2、写dao层,接口,接口实现,基本类pojo作为dao层方法的参数
3、写service层,接口,方法,dao层类作为属性,service方法使用dao层方法
4、写impl层,基本类pojo和service层类作为属性,impl方法使用service层方法,pojo作为参数传递pojo:
属性:题目需要
方法:题目需要dao:
无属性
pojo作为方法参数service:
属性:dao
方法传递dao参数,使用dao方法Impl:
属性:pojo、service
方法传递service参数,使用service方法有属性必有get、set函数
//User.java
package com.ali.pojo;public class User {//描述user表String name;String pwd;int age;
public String getName() {return name;
}
public void setName(String name) {this.name = name;
}
public String getPwd() {return pwd;
}
public void setPwd(String pwd) {this.pwd = pwd;
}
public int getAge() {return age;
}
public void setAge(int age) {this.age = age;
}}
//UserDao.java
package com.ali.dao;import com.ali.pojo.User;public interface UserDao {int regUser(User user);int loginUser(User user);
}
//UserDaoImpl.java
package com.ali.dao;import com.ali.pojo.User;public class UserDaoImpl implements UserDao {@Overridepublic int regUser(User user) {// TODO Auto-generated method stubSystem.out.println(user.getName());return 1;}@Overridepublic int loginUser(User user) {// TODO Auto-generated method stubreturn 0;}}
//UserService.java
package com.ali.service;import com.ali.pojo.User;public interface UserService {int regUser(User user);int loginUser(User user);
}
//UserServiceImpl.java
package com.ali.service;import com.ali.dao.UserDaoImpl;
import com.ali.pojo.User;public class UserServiceImpl implements UserService {UserDaoImpl userDaoImpl;public UserDaoImpl getUserDaoImpl() {return userDaoImpl;
}public void setUserDaoImpl(UserDaoImpl userDaoImpl) {this.userDaoImpl = userDaoImpl;
}@Overridepublic int regUser(User user) {// TODO Auto-generated method stubuserDaoImpl.regUser(user);return 1;}@Overridepublic int loginUser(User user) {// TODO Auto-generated method stubreturn 0;}}
//UserCtrl.java
package com.ali.ctrl;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ali.pojo.User;
import com.ali.service.UserServiceImpl;public class UserCtrl {UserServiceImpl userServiceImpl;User user;public UserServiceImpl getUserServiceImpl() {return userServiceImpl;}public void setUserServiceImpl(UserServiceImpl userServiceImpl) {this.userServiceImpl = userServiceImpl;}public static void main(String[] args) {ClassPathXmlApplicationContext cfg=new ClassPathXmlApplicationContext("applicationContext.xml");UserCtrl userctrl=(UserCtrl)cfg.getBean("userctrl");userctrl.reg();
}public User getUser() {return user;
}public void setUser(User user) {this.user = user;
}int reg(){ userServiceImpl.regUser(user);System.out.println("yes....");return 1;}
}
<!--applicationContext.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsd"><!--创建基本类--><bean id="user" class="com.ali.pojo.User"><property name="name" value="user0"></property></bean><!--基本类作为方法参数,记本类为类1--><bean id="userdaoimpl" class="com.ali.dao.UserDaoImpl"></bean> <!--类1作为属性,记本类为类2,这时候类2的方法就使用类1的方法--><bean id="userserviceimpl" class="com.ali.service.UserServiceImpl"> <property name="userDaoImpl" ref="userdaoimpl"></property></bean><!--基本类和类2作为属性,基本类为类3,这时候类3的方法使用类2的方法,类2的方法使用类1的方法,类1的方法需要基本类作为参数,传入即可--><bean id="userctrl" class="com.ali.ctrl.UserCtrl"><property name="user" ref="user"></property><property name="userServiceImpl" ref="userserviceimpl"></property></bean></beans>