2023年3月

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
> <!-- 基本类型--> <beanid="people1"class="com.java1234.entity.People"> <propertyname="id"value="1"></property> <propertyname="name"value="张三"></property> <propertyname="age"value="11"></property> </bean> <!--注入bean --> <beanid="dog1"class="com.java1234.entity.Dog"> <propertyname="name"value="Jack"></property> </bean> <beanid="people2"class="com.java1234.entity.People"> <propertyname="id"value="1"></property> <propertyname="name"value="张三"></property> <propertyname="age"value="11"></property> <propertyname="dog"ref="dog1"></property> </bean> <!--内部bean --> <beanid="people3"class="com.java1234.entity.People"> <propertyname="id"value="1"></property> <propertyname="name"value="张三"></property> <propertyname="age"value="11"></property> <propertyname="dog"> <beanclass="com.java1234.entity.Dog"> <propertyname="name"value="Tom"></property> </bean> </property> </bean> <!-- null值--> <beanid="people4"class="com.java1234.entity.People"> <propertyname="id"value="1"></property> <propertyname="name"value="张三"></property> <propertyname="age"value="11"></property> <propertyname="dog"> <null></null> </property> </bean> <!-- 级联属性--> <!--<bean id="people5" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog.name" value="Jack2"></property>
</bean>
--> <!-- 集合属性--> <beanid="people6"class="com.java1234.entity.People"> <propertyname="id"value="1"></property> <propertyname="name"value="张三"></property> <propertyname="age"value="11"></property> <propertyname="dog"ref="dog1"></property> <propertyname="hobbies"> <list> <value>唱歌</value> <value>跳舞</value> </list> </property> <propertyname="loves"> <!--set是不可以重复的 --> <set> <value>唱歌2</value> <value>跳舞2</value> </set> </property> <propertyname="works"> <!--键值对 --> <map> <entry> <key><value>上午</value></key> <value>写代码</value> </entry> <entry> <key><value>下午</value></key> <value>测试代码</value> </entry> </map> </property> <propertyname="addresses"> <!--属性 一般用于系统配置 --> <props> <propkey="address1">aaaaa</prop> <propkey="address2">bbbbb</prop> </props> </property> </bean> </beans>

private List<String> hobbies=new ArrayList<String>();
private Set<String> loves=new HashSet<String>();
private Map<String,String> works=new HashMap<String,String>();
private Properties addresses=new Properties();

或第六讲前也是讲此知识。

\[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]《一头扎进Spring4》第六讲 源码

继承的xml配置方式:

    <beanid="abstractPeople"class="com.java1234.entity.People"abstract="true">
        <propertyname="className"value="高三5班"></property>
        <propertyname="age"value="19"></property>
    </bean>
    
    <beanid="zhangsan"parent="abstractPeople"depends-on="autority"><!--依赖 会优先到后方获取autority bean-->
        <propertyname="id"value="1"></property>
        <propertyname="name"value="张三"></property>
    </bean>
    
    <beanid="lisi"parent="abstractPeople">
        <propertyname="id"value="2"></property>
        <propertyname="name"value="李四"></property>
        <propertyname="age"value="20"></property> <!--重写了此属性 -->
    </bean>
<beanid="autority"class="com.java1234.service.Authority"></bean>

<beanid="dog"class="com.java1234.entity.Dog"scope="singleton">
        <propertyname="name"value="jack"></property>
    </bean>

[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]《一头扎进Spring4》第七讲 源码

[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]《一头扎进Spring4》第八讲 源码

配置:以下重点划出的是新添加进去的

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
"
>

、、切面配置如下

    <beanid="studentServiceAspect"class="com.java1234.advice.StudentServiceAspect"></bean>//先创建好两个bean<beanid="studentService"class="com.java1234.service.impl.StudentServiceImpl"></bean>
    
    <aop:config>//config  aop标志<aop:aspectid="studentServiceAspect"ref="studentServiceAspect">//aspect定义一个切面,studentServiceAspect是切面类
//定义切点,是方法级别的,要写表达式{第一个*表示任何是任意,后面的两点..表示参数是任意的},指定方法
<aop:pointcutexpression="execution(* com.java1234.service.*.*(..))"id="businessService"/> <aop:beforemethod="doBefore"pointcut-ref="businessService"/> <aop:aftermethod="doAfter"pointcut-ref="businessService"/> <aop:aroundmethod="doAround"pointcut-ref="businessService"/>//环绕<aop:after-returningmethod="doAfterReturning"pointcut-ref="businessService"/>//返回<aop:after-throwingmethod="doAfterThrowing"pointcut-ref="businessService"throwing="ex"/>//异常</aop:aspect> </aop:config>

packagecom.java1234.advice;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;public classStudentServiceAspect {public voiddoBefore(JoinPoint jp){
System.out.println(
"类名:"+jp.getTarget().getClass().getName());
System.out.println(
"方法名:"+jp.getSignature().getName());
System.out.println(
"开始添加学生:"+jp.getArgs()[0]);
}
public voiddoAfter(JoinPoint jp){
System.out.println(
"类名:"+jp.getTarget().getClass().getName());
System.out.println(
"方法名:"+jp.getSignature().getName());
System.out.println(
"学生添加完成:"+jp.getArgs()[0]);
}
public Object doAround(ProceedingJoinPoint pjp) throwsThrowable{
System.out.println(
"添加学生前");
Object retVal
=pjp.proceed();
System.out.println(retVal);
System.out.println(
"添加学生后");returnretVal;
}
public voiddoAfterReturning(JoinPoint jp){
System.out.println(
"返回通知");
}
public voiddoAfterThrowing(JoinPoint jp,Throwable ex){
System.out.println(
"异常通知");
System.out.println(
"异常信息:"+ex.getMessage());
}
}

\[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]《一头扎进Spring4》第九讲 源码\Spring404

此例子有对数据库的增删改查
CRUD
是指增加(Create)、读取查询(Retrieve)、更新(Update)和删除(Delete)

说它落后,因为到了

1、人人框架的xml里面根本上都看不到这些配置了:【但是比将数据库配置写在代码中要好】

2、在Dao 和Service的Impl实现中也不用在java代码里面写 SQL语句。sql语句写在xml里面

课件内容

bean.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
"
> <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <propertyname="driverClassName"value="${jdbc.driverClassName}"/> <propertyname="url"value="${jdbc.url}"/> <propertyname="username"value="${jdbc.username}"/> <propertyname="password"value="${jdbc.password}"/> </bean> <context:property-placeholderlocation="jdbc.properties"/>//加载本地的文件,文件如下下发 <beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"> <propertyname="dataSource"ref="dataSource"></property> </bean> <beanid="studentDao"class="com.java1234.dao.impl.StudentDaoImpl"> <propertyname="jdbcTemplate"ref="jdbcTemplate"></property>//注入属性,然后在StudentDaoImpl就可以用Jdbc了 </bean> <beanid="studentService"class="com.java1234.service.impl.StudentServiceImpl"> <propertyname="studentDao"ref="studentDao"></property>//StudentServiceImpl类中要有studentDao属性和get、set方法,在这里注入属性,同上 </bean> </beans>

jdbc.properties文件:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_spring
jdbc.username=root
jdbc.password=123456

渐进式优化:

1、 直接用jdbc来链接,很麻烦try{
conn
=dataSource.getConnection();
PreparedStatement ps
=conn.prepareStatement(sql);
ps.setString(
1, u.getName());
ps.setString(
2, u.getPassword());
ps.executeUpdate();
ps.close();
}
catch(SQLException e) {throw newRuntimeException(e);2、JdbcTemplatepublic voidinsert(User u) {
String sql
= "insert into _user " + "values(null, ?, ?)";//普通的sql语句 JdbcTemplate template = newJdbcTemplate(dataSource);
template.update(sql,
newObject[]{u.getName(), u.getPassword()});
}
3、JdbcDaoSupport
这个更简单,不用new JdbcTemplate了。
this.getJdbcTemplate().updatepublic voidinsert(User u) {
String sql
= "insert into _user " + "values(null, ?, ?)";//普通的sql语句 this.getJdbcTemplate().update(sql, newObject[]{u.getName(), u.getPassword()});
}
4、namedParameterJdbcTemplate 不用?好,用冒号加变量名
如:namedParameterJdbcTemplate
= newNamedParameterJdbcTemplate(jdbcTemplate);
String selectSql
= "select * from test where name=:name";
String deleteSql
= "delete from test where name=:name";
Map
<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put(
"name", "name5");
namedParameterJdbcTemplate.update(insertSql, paramMap);
//要传一个Map 进去。MapSqlparameterSource类型的也可以

=========例子结构==========

\src\jdbc.properties;//数据库配置
\src\beans.xml;//配置文件
\src\com\java1234\model\Student.java;//实体类
\src\com\java1234\dao\StudentDao.java;//dao接口
\src\com\java1234\dao\impl\StudentDaoImpl.java;//dao实现
\src\com\java1234\service\StudentService.java;//业务接口
\src\com\java1234\service\impl\StudentServiceImpl.java;//业务实现
\src\com\java1234\test\T.java;//调用层,可以当做UI展示层


packagecom.java1234.dao.impl;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.core.RowCallbackHandler;importcom.java1234.dao.StudentDao;importcom.java1234.model.Student;public class StudentDaoImpl implementsStudentDao{privateJdbcTemplate jdbcTemplate;public voidsetJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate =jdbcTemplate;
}

@Override
public intaddStudent(Student student) {
String sql
="insert into t_student values(null,?,?)";
Object []params
=newObject[]{student.getName(),student.getAge()};returnjdbcTemplate.update(sql,params);
}

@Override
public intupdateStudent(Student student) {
String sql
="update t_student set name=?,age=? where id=?";
Object []params
=newObject[]{student.getName(),student.getAge(),student.getId()};returnjdbcTemplate.update(sql,params);
}

@Override
public int deleteStudent(intid) {
String sql
="delete from t_student where id=?";
Object []params
=newObject[]{id};returnjdbcTemplate.update(sql,params);
}

@Override
public List<Student>findStudents() {
String sql
="select * from t_student";final List<Student> studentList=new ArrayList<Student>();
jdbcTemplate.query(sql,
newRowCallbackHandler(){

@Override
public void processRow(ResultSet rs) throwsSQLException {
Student student
=newStudent();
student.setId(rs.getInt(
"id"));
student.setName(rs.getString(
"name"));
student.setAge(rs.getInt(
"age"));
studentList.add(student);
}

});
returnstudentList;
}

}

StudentDaoImpl.java;


packagecom.java1234.test;importjava.util.List;importorg.junit.Before;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.java1234.model.Student;importcom.java1234.service.StudentService;public classT {privateApplicationContext ac;

@Before
public void setUp() throwsException {
ac
=new ClassPathXmlApplicationContext("beans.xml");
}

@Test
public voidaddStudent() {
StudentService studentService
=(StudentService)ac.getBean("studentService");int addNums=studentService.addStudent(new Student("����", 1));if(addNums==1){
System.out.println(
"��ӳɹ�");
}
}

@Test
public voidupdateStudent() {
StudentService studentService
=(StudentService)ac.getBean("studentService");int updateNums=studentService.updateStudent(new Student(8,"����2", 2));if(updateNums==1){
System.out.println(
"���³ɹ�");
}
}

@Test
public voiddeleteStudent() {
StudentService studentService
=(StudentService)ac.getBean("studentService");int deleteNums=studentService.deleteStudent(8);if(deleteNums==1){
System.out.println(
"ɾ���ɹ�");
}
}

@Test
public voidfindStudents() {
StudentService studentService
=(StudentService)ac.getBean("studentService");
List
<Student> studentList=studentService.findStudents();for(Student student:studentList){
System.out.println(student);
}
}
}

T.java;//调用层

《一头扎进SpringMvc视频教程\《一头扎进SpringMvc》第四讲 源码\》

对象自动转换为json格式要在 spring-mvc.xml添加一个东西 ,和对应的命名空间引用和规范,和对应的jar包

<!-- 支持对象与json的转换。 -->
<mvc:annotation-driven/>
//注解驱动

提到目前已经引用了一堆很乱的jar包,maven时就简单了。

<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd"
> <!--支持对象与json的转换。--> <mvc:annotation-driven/>

对应的jar包

jackson-annotations-2.2.1.jar
jackson-core-2.2.1.jar
jackson-core-asl-1.8.8.jar
jackson-databind-2.2.1.jar
jackson-mapper-asl-1.8.8.jar
jackson-module-jaxb-annotations-2.2.1.jar