使用SSH框架编写学生信息:
一.新建Java工程:
(1)建立好Java各层级之间的结构:业务处理层dao,数据模型层domain,页面请求处理层(Struts2 MVC层)action,service层.
(2)建立好各层的实现类及接口;
(3)建立一个source folder文件夹,用来存放一些配置问价.
(4)改变字节码生成的位置,改为WEB-INF下面的classes文件夹下.
Java工程层级结构如下图:
二.hibernate整合到Spring容器中
步骤:
1.编写domain,数据模型,此例子为Student类.
package com.xk.ssh.proj.domian; public class Student { private Integer id; private String name; private Integer age; private double fees; private long contact; public Student() { } 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public double getFees() { return fees; } public void setFees(double fees) { this.fees = fees; } public long getContact() { return contact; } public void setContact(long contact) { this.contact = contact; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", fees=" + fees + ", contact=" + contact + "]"; } public Student(Integer id, String name, Integer age, double fees, long contact) { this.id = id; this.name = name; this.age = age; this.fees = fees; this.contact = contact; } public Student( String name, Integer age, double fees, long contact) { this.name = name; this.age = age; this.fees = fees; this.contact = contact; } public Student(Integer id) { this.id = id; } }
2.遍写映射文件,在domain文件夹下编写Student.xml文件.hibernate主要用来管理数据库.
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.xk.ssh.proj.domian"> <class name="Student" table="Student"> <id name="id" column="s_id"> <generator class="native" /> </id> <property name="name" column="s_name" /> <property name="age" column="s_age" /> <property name="fees" column="s_fees" /> <property name="contact" column="s_phone" /> </class> </hibernate-mapping>
3.编写Dao接口及Dao的实现类
package com.xk.ssh.proj.dao; import java.util.List; import com.xk.ssh.proj.domian.Student; public interface IStudentDao { public void save(Student stu); public void delete(Student stu); public void update(Student stu); public Student get(Student stu); public List<Student> list(); }
package com.xk.ssh.proj.dao.impl; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.xk.ssh.proj.dao.IStudentDao; import com.xk.ssh.proj.domian.Student; public class StudentDaoImpl implements IStudentDao { // 在Dao中注入SessionFaction private SessionFactory sessionfactory; public void setSessionfactory(SessionFactory sessionfactory) { this.sessionfactory = sessionfactory; } @Override public void save(Student stu) { Session session = sessionfactory.getCurrentSession(); session.save(stu); } @Override public void delete(Student stu) { Session session = sessionfactory.getCurrentSession(); session.delete(stu); } @Override public void update(Student stu) { Session session = sessionfactory.getCurrentSession(); session.update(stu); } @Override public Student get(Student stu) { Session session = sessionfactory.getCurrentSession(); // Student.class:Student的字节码文件 return (Student) session.get(Student.class, stu.getId()); } @SuppressWarnings("unchecked") @Override public List<Student> list() { // 查询session.createQuery("sql语句").list Session session = sessionfactory.getCurrentSession(); return session.createQuery("SELECT stu FROM Student stu").list(); } }
4.拷贝架包:数据驱动的hibernate包,Spring的core,test,bean,context等
antlr-2.7.7.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
dom4j-1.6.1.jar
druid-1.0.15.jar
ehcache-core-2.4.3.jar
freemarker-2.3.22.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.2.0.Final.jar
hibernate-ehcache-4.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
mysql-connector-java-5.1.40-bin.jar
ognl-3.0.6.jar
servlet-api.jar
slf4j-api-1.6.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-test-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
struts2-core-2.3.24.jar
struts2-spring-plugin-2.3.24.jar
xwork-core-2.3.24.jar
5.Spring的配置文件:
①添加并配置.properties文件
<context:property-placeholder location="classpath:db.properties" />
②配置DataSource,连接数据库
<!-- 配置DataSource --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!-- 连接数据库 --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
④配置SessionFactory:(jdbc/tx/orm架包)
使用LocalSessionFactoryBean 创建SessionFactory
配置dataSource和引用
配置hibernate中的常规配置(方言)
配置domain的映射文件
<!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 配置连接数据库的信息 此处的name是org.springframework.orm.hibernate4.LocalSessionFactoryBean类中的dataSource 此处的ref是引用配置DateSource中的id --> <property name="dataSource" ref="dataSource" /> <!-- 配置hibernate常规配置 --> <property name="hibernateProperties"> <!-- dialect方言 --> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=none hibernate.show_sql=true </value> </property> <!-- 配置映射文件,告诉Spring框架*.hbm.xml在哪 此处的name是org.springframework.orm.hibernate4.LocalSessionFactoryBean类中的mappingDirectoryLocations value是自己的对象xml的包的权限定名称 --> <property name="mappingDirectoryLocations" value="classpath:com/xk/ssh/proj/domian" /> </bean>
6.编写Dao
①直接在Dao中注入一个SessionFactory;
private SessionFactory sessionfactory; public void setSessionfactory(SessionFactory sessionfactory) { this.sessionfactory = sessionfactory; }
②在Dao中直接使用SessionFactory.getCurrentSession()来得到我们需要的session
Session session = sessionfactory.getCurrentSession();
7.在Spring中配置Dao
①抽象一个baseDao;
<bean id = "baseDao" abstract = "true" />
②Dao继承BaseDao;
<bean id="stuDao" parent="baseDao"/>
Dao类extends jdbcDaoSupport implements 接口
8.编写Service
①编写Service接口和实现
package com.xk.ssh.proj.service; import java.util.List; import com.xk.ssh.proj.domian.Student; public interface IStudentService { public void save(Student stu); public void delete(Student stu); public void update(Student stu); public Student get(Student stu); public List<Student> list(); }
1 package com.xk.ssh.proj.service.impl; 2 import java.util.List; 3 import com.xk.ssh.proj.dao.IStudentDao; 4 import com.xk.ssh.proj.domian.Student; 5 import com.xk.ssh.proj.service.IStudentService; 6 public class StudentServiceImpl implements IStudentService { 7 private IStudentDao student; 8 public void setStudent(IStudentDao student) { 9 this.student = student; 10 } 11 @Override 12 public void save(Student stu) { 13 student.save(stu); 14 } 15 @Override 16 public void delete(Student stu) { 17 student.delete(stu); 18 } 19 @Override 20 public void update(Student stu) { 21 student.update(stu); 22 } 23 @Override 24 public Student get(Student stu) { 25 return student.get(stu); 26 } 27 @Override 28 public List<Student> list() { 29 return student.list(); 30 } 31 }
②在Spring中配置事物servicebean
<!-- 配置service id为唯一标识,class为service的实现类 name为service实现类中注入的dao层对象 ref为引用Dao --> <bean id="stuService" class="com.xk.ssh.proj.service.impl.StudentServiceImpl"> <property name="student" ref="stuDao" /> </bean>
9.配置事物
<!-- 配置事务管理器 --> <bean id="txMgr" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事物 事物是配置给service实现的 --> <aop:config> <!-- pointcut:切点 ; Advice:通知 advisor:切点和通知的配置器 下面是配置切点:advice-ref:引用通知的ID --> <aop:pointcut expression="execution(* com.xk.ssh.proj.service.*.*(..))" id="stuTC" /> <aop:advisor advice-ref="StuAdvice" pointcut-ref="stuTC" /> </aop:config> <tx:advice id="StuAdvice" transaction-manager="txMgr"> <tx:attributes> <!-- 过滤方法,将所有以get和list开头的方法标记为只读 --> <tx:method name="get*" read-only="true" /> <tx:method name="list*" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice>
三.将Struts2 集成在Spring容器
1.导架包:(spring-web/Struts-spring-plugin...jar)
2.编写Struts.xml(位置与源文件夹同级)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="action,nice,,"></constant> <!-- 加后缀 --> <constant name="struts.devMode" value="true" /> <!-- 小的改动不需要再启动服务器 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 动态调用 --> <package name="hello" extends="struts-default"> <action name="list_*" class="StuAction" method="{1}"> <result name="list">/WEB-INF/views/SSH.jsp</result> </action> </package> </struts>
3.编写web.xml文件(位置在WEB-INF文件夹下面)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="action,nice,,"></constant> <!-- 加后缀 --> <constant name="struts.devMode" value="true" /> <!-- 小的改动不需要再启动服务器 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 动态调用 --> <package name="hello" extends="struts-default"> <action name="list_*" class="StuAction" method="{1}"> <result name="list">/WEB-INF/views/SSH.jsp</result> </action> </package> </struts>
4.编写Action (页面请求)
package com.xk.ssh.proj.mvc; import java.util.List; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.xk.ssh.proj.domian.Student; import com.xk.ssh.proj.service.IStudentService; public class StudentAction extends ActionSupport { private static final long serialVersionUID = 1L; private IStudentService studentService; public void setStudentService(IStudentService studentService) { this.studentService = studentService; } public String list() { List<Student> list = studentService.list(); ActionContext context = ActionContext.getContext(); context.put("key", list); System.out.println(list); return "list"; } }
5.在applicationcontext.xml中配置action
<!-- 配置Action --> <bean id="StuAction" class="com.xk.ssh.proj.mvc.StudentAction"> <property name="studentService" ref="stuService"/> </bean>
6.编写Struts的配置文件:在action的class属性中填写的是此action在applicationcontext.xml中对应的bean的id
7.在web.xml中添加listener监听器
8.在web.xml中添加Spring框架启动的加载文件配置路径
<context-param > <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </context-param>
四.附上完整代码:
1 //domain 2 package com.xk.ssh.proj.domian; 3 4 public class Student { 5 private Integer id; 6 private String name; 7 private Integer age; 8 private double fees; 9 private long contact; 10 public Student() { 11 } 12 public Integer getId() { 13 return id; 14 } 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public Integer getAge() { 25 return age; 26 } 27 public void setAge(Integer age) { 28 this.age = age; 29 } 30 public double getFees() { 31 return fees; 32 } 33 public void setFees(double fees) { 34 this.fees = fees; 35 } 36 public long getContact() { 37 return contact; 38 } 39 public void setContact(long contact) { 40 this.contact = contact; 41 } 42 @Override 43 public String toString() { 44 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", fees=" + fees + ", contact=" + contact 45 + "]"; 46 } 47 public Student(Integer id, String name, Integer age, double fees, long contact) { 48 this.id = id; 49 this.name = name; 50 this.age = age; 51 this.fees = fees; 52 this.contact = contact; 53 } 54 public Student( String name, Integer age, double fees, long contact) { 55 this.name = name; 56 this.age = age; 57 this.fees = fees; 58 this.contact = contact; 59 } 60 public Student(Integer id) { 61 this.id = id; 62 } 63 } 64 65 //domain的xml 66 <?xml version="1.0"?> 67 <!DOCTYPE hibernate-mapping PUBLIC 68 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 69 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 70 <hibernate-mapping package="com.xk.ssh.proj.domian"> 71 <class name="Student" table="Student"> 72 <id name="id" column="s_id"> 73 <generator class="native" /> 74 </id> 75 <property name="name" column="s_name" /> 76 <property name="age" column="s_age" /> 77 <property name="fees" column="s_fees" /> 78 <property name="contact" column="s_phone" /> 79 </class> 80 </hibernate-mapping> 81 //dao接口 82 package com.xk.ssh.proj.dao; 83 import java.util.List; 84 import com.xk.ssh.proj.domian.Student; 85 public interface IStudentDao { 86 public void save(Student stu); 87 public void delete(Student stu); 88 public void update(Student stu); 89 public Student get(Student stu); 90 public List<Student> list(); 91 } 92 93 //dao 94 package com.xk.ssh.proj.dao.impl; 95 96 import java.util.List; 97 98 import org.hibernate.Session; 99 import org.hibernate.SessionFactory; 100 101 102 import com.xk.ssh.proj.dao.IStudentDao; 103 import com.xk.ssh.proj.domian.Student; 104 105 public class StudentDaoImpl implements IStudentDao { 106 // 在Dao中注入SessionFaction 107 private SessionFactory sessionfactory; 108 public void setSessionfactory(SessionFactory sessionfactory) { 109 this.sessionfactory = sessionfactory; 110 } 111 @Override 112 public void save(Student stu) { 113 Session session = sessionfactory.getCurrentSession(); 114 session.save(stu); 115 } 116 @Override 117 public void delete(Student stu) { 118 Session session = sessionfactory.getCurrentSession(); 119 session.delete(stu); 120 } 121 @Override 122 public void update(Student stu) { 123 Session session = sessionfactory.getCurrentSession(); 124 session.update(stu); 125 } 126 @Override 127 public Student get(Student stu) { 128 Session session = sessionfactory.getCurrentSession(); 129 // Student.class:Student的字节码文件 130 return (Student) session.get(Student.class, stu.getId()); 131 } 132 @SuppressWarnings("unchecked") 133 @Override 134 public List<Student> list() { 135 // 查询session.createQuery("sql语句").list 136 Session session = sessionfactory.getCurrentSession(); 137 return session.createQuery("SELECT stu FROM Student stu").list(); 138 } 139 } 140 //service接口 141 package com.xk.ssh.proj.service; 142 import java.util.List; 143 import com.xk.ssh.proj.domian.Student; 144 public interface IStudentService { 145 public void save(Student stu); 146 public void delete(Student stu); 147 public void update(Student stu); 148 public Student get(Student stu); 149 public List<Student> list(); 150 } 151 152 //service类 153 package com.xk.ssh.proj.service.impl; 154 import java.util.List; 155 import com.xk.ssh.proj.dao.IStudentDao; 156 import com.xk.ssh.proj.domian.Student; 157 import com.xk.ssh.proj.service.IStudentService; 158 public class StudentServiceImpl implements IStudentService { 159 private IStudentDao student; 160 public void setStudent(IStudentDao student) { 161 this.student = student; 162 } 163 @Override 164 public void save(Student stu) { 165 student.save(stu); 166 } 167 @Override 168 public void delete(Student stu) { 169 student.delete(stu); 170 } 171 @Override 172 public void update(Student stu) { 173 student.update(stu); 174 } 175 @Override 176 public Student get(Student stu) { 177 return student.get(stu); 178 } 179 @Override 180 public List<Student> list() { 181 return student.list(); 182 } 183 } 184 185 //MVC:action类 186 package com.xk.ssh.proj.mvc; 187 import java.util.List; 188 import com.opensymphony.xwork2.ActionContext; 189 import com.opensymphony.xwork2.ActionSupport; 190 import com.xk.ssh.proj.domian.Student; 191 import com.xk.ssh.proj.service.IStudentService; 192 public class StudentAction extends ActionSupport { 193 private static final long serialVersionUID = 1L; 194 private IStudentService studentService; 195 public void setStudentService(IStudentService studentService) { 196 this.studentService = studentService; 197 } 198 public String list() { 199 List<Student> list = studentService.list(); 200 ActionContext context = ActionContext.getContext(); 201 context.put("key", list); 202 System.out.println(list); 203 return "list"; 204 } 205 } 206 //Struts.xml 207 <?xml version="1.0" encoding="UTF-8" ?> 208 <!DOCTYPE struts PUBLIC 209 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 210 "http://struts.apache.org/dtds/struts-2.3.dtd"> 211 <struts> 212 <constant name="struts.action.extension" value="action,nice,,"></constant> <!-- 加后缀 --> 213 <constant name="struts.devMode" value="true" /> <!-- 小的改动不需要再启动服务器 --> 214 <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 动态调用 --> 215 216 <package name="hello" extends="struts-default"> 217 <action name="list_*" class="StuAction" method="{1}"> 218 <result name="list">/WEB-INF/views/SSH.jsp</result> 219 </action> 220 </package> 221 </struts> 222 223 //applicationcontext.xml 224 <?xml version="1.0" encoding="UTF-8"?> 225 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 226 xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" 227 xmlns:context="http://www.springframework.org/schema/context" 228 xmlns:aop="http://www.springframework.org/schema/aop" 229 xsi:schemaLocation="http://www.springframework.org/schema/beans 230 http://www.springframework.org/schema/beans/spring-beans.xsd 231 http://www.springframework.org/schema/context 232 http://www.springframework.org/schema/context/spring-context.xsd 233 http://www.springframework.org/schema/aop 234 http://www.springframework.org/schema/aop/spring-aop.xsd 235 http://www.springframework.org/schema/tx 236 http://www.springframework.org/schema/tx/spring-tx.xsd"> 237 <!-- 配置数据库连接池 --> 238 <context:property-placeholder location="classpath:db.properties" /> 239 240 <!-- 配置DataSource --> 241 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 242 destroy-method="close"> 243 <!-- 连接数据库 --> 244 <property name="driverClassName" value="${jdbc.driverClassName}" /> 245 <property name="url" value="${jdbc.url}" /> 246 <property name="username" value="${jdbc.username}" /> 247 <property name="password" value="${jdbc.password}" /> 248 </bean> 249 250 <!-- 配置sessionFactory --> 251 <bean id="sessionFactory" 252 class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 253 254 <!-- 配置连接数据库的信息 此处的name是org.springframework.orm.hibernate4.LocalSessionFactoryBean类中的dataSource 255 此处的ref是引用配置DateSource中的id --> 256 <property name="dataSource" ref="dataSource" /> 257 258 <!-- 配置hibernate常规配置 --> 259 <property name="hibernateProperties"> 260 <!-- dialect方言 --> 261 <value> 262 hibernate.dialect=org.hibernate.dialect.MySQLDialect 263 hibernate.hbm2ddl.auto=none 264 hibernate.show_sql=true 265 </value> 266 </property> 267 268 <!-- 配置映射文件,告诉Spring框架*.hbm.xml在哪 此处的name是org.springframework.orm.hibernate4.LocalSessionFactoryBean类中的mappingDirectoryLocations 269 value是自己的对象xml的包的权限定名称 --> 270 <property name="mappingDirectoryLocations" value="classpath:com/xk/ssh/proj/domian" /> 271 </bean> 272 273 <!-- 配置Dao: 此处ID为唯一标识;class为实现类的权限定 --> 274 <bean id="stuDao" class="com.xk.ssh.proj.dao.impl.StudentDaoImpl"> 275 <!-- 此处name为StudentDaoimpl类中的注入的sessionFactory ; ref为session工厂的标识 --> 276 <property name="sessionfactory" ref="sessionFactory" /> 277 </bean> 278 279 <!-- 配置service id为唯一标识,class为service的实现类 name为service实现类中注入的dao层对象 ref为引用Dao --> 280 <bean id="stuService" class="com.xk.ssh.proj.service.impl.StudentServiceImpl"> 281 <property name="student" ref="stuDao" /> 282 </bean> 283 284 <!-- 配置事务管理器 --> 285 <bean id="txMgr" 286 class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 287 <property name="sessionFactory" ref="sessionFactory" /> 288 </bean> 289 290 <!-- 配置事物 事物是配置给service实现的 --> 291 <aop:config> 292 <!-- pointcut:切点 ; Advice:通知 advisor:切点和通知的配置器 下面是配置切点:advice-ref:引用通知的ID --> 293 <aop:pointcut expression="execution(* com.xk.ssh.proj.service.*.*(..))" 294 id="stuTC" /> 295 <aop:advisor advice-ref="StuAdvice" pointcut-ref="stuTC" /> 296 </aop:config> 297 298 <tx:advice id="StuAdvice" transaction-manager="txMgr"> 299 <tx:attributes> 300 <!-- 过滤方法,将所有以get和list开头的方法标记为只读 --> 301 <tx:method name="get*" read-only="true" /> 302 <tx:method name="list*" read-only="true" /> 303 <tx:method name="*" /> 304 </tx:attributes> 305 </tx:advice> 306 <!-- 配置Action --> 307 <bean id="StuAction" class="com.xk.ssh.proj.mvc.StudentAction"> 308 <property name="studentService" ref="stuService"/> 309 </bean> 310 311 </beans> 312 //test测试类 313 package com.xk.ssh.proj.test; 314 315 import java.util.List; 316 317 import org.junit.Test; 318 import org.junit.runner.RunWith; 319 import org.springframework.beans.factory.annotation.Autowired; 320 import org.springframework.test.context.ContextConfiguration; 321 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 322 323 import com.xk.ssh.proj.domian.Student; 324 import com.xk.ssh.proj.service.IStudentService; 325 326 @RunWith(SpringJUnit4ClassRunner.class) 327 @ContextConfiguration("classpath:applicationcontext.xml") 328 public class StudentTest { 329 @Autowired 330 private IStudentService stuService; 331 @Test 332 public void TestStudentSave() throws Exception { 333 stuService.save(new Student("张1", 11, 1991, 2222)); 334 stuService.save(new Student("张2", 12, 1992, 2222)); 335 stuService.save(new Student("张3", 13, 1993, 2222)); 336 stuService.save(new Student("张4", 14, 1994, 2222)); 337 } 338 @Test 339 public void TestStudentDelete() throws Exception { 340 stuService.delete(new Student(1)); 341 } 342 @Test 343 public void TestStudentUptade() throws Exception { 344 stuService.update(new Student(2, "张阿三", 2, 1123, 12232)); 345 } 346 @Test 347 public void TestStudentGet() throws Exception { 348 Student student = stuService.get(new Student(2)); 349 System.out.println(student); 350 } 351 @Test 352 public void TestStudentList() throws Exception { 353 List<Student> list = stuService.list(); 354 System.out.println(list); 355 } 356 } 357 //properties文件 358 jdbc.driverClassName=com.mysql.jdbc.Driver 359 jdbc.url=jdbc:mysql:///spring 360 jdbc.username=root 361 jdbc.password=root