发表于: 2017-08-10 23:10:27
1 1029
今天完成的事情:总结了这几天学习Spring的心得 拿一个最简单的自己写的helloworld来看spring的基本结构
先写一个实体类 声明message set get方法 直接打印message
package com.jnshu;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message=message;
}
public void getMessage() {
System.out.println("your message:"+message);
}
}
写个spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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-3.0.xsd">
<bean id="helloWorld" class="com.jnshu.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
这里就是spring的核心配置 bean id是唯一标示 class是类 property属性 把值赋给name的属性
测试
public class HelloworldTest {
@Test
public void helloTest(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld h = (HelloWorld) ctx.getBean("helloWorld");
h.getMessage();
}
}
Application Context 是 spring 中较高级的容器。和 BeanFactory 类似,它可以加载配置文件中定义的 bean,将所有的 bean 集中在一起,当有请求的时候分配 bean。
- FileSystemXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径
- ClassPathXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你不需要提供 XML 文件的完整路径,只需正确配置 CLASSPATH 环境变量即可,因为,容器会从 CLASSPATH 中搜索 bean 配置文件。
- WebXmlApplicationContext:该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean。
这里用的是最常见的第一种方法 (“ ”) 里面是Spring的配置文件的名
- 然后使用已创建的上下文的 getBean() 方法来获得所需的 bean。这个方法使用 bean 的 ID 返回一个最终可以转换为实际对象的通用对象。一旦有了对象,你就可以使用这个对象调用任何类的方法。这里是h 再调用方法getMessage 直接打印 message
- 这里bean是Spring的核心理念
- bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。这些 bean 是由用容器提供的配置元数据创建的
写数据源配置 mapper文件配置
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="Dao.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>- Spring可以管理数据源 如下
<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
<property name="username" value="root"/>
<property name="password" value="password"/></bean>
用数据库池依赖一下 配置文件写一下就好也可以写db.properties管理
昨天程序今天测试成功
import Dao.UserMapper;
import model.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.TestExecutionListeners;
public class test2 {
@Test
public void supe() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) ctx.getBean("userMapper");
User u = new User();
u.setUsername("admin");
u.setPassword("admin");
System.out.println(userMapper.selectUser(u));
}
@Test
public void add() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) ctx.getBean("userMapper");
User insertUser = new User();
insertUser.setUsername("testUsername");
insertUser.setPassword("testPassword");
userMapper.insertUser(insertUser);
}
明天计划的事情:配置一下服务器试验一下 听师兄说都是坑 尤其是mysql很难装好
遇到的问题:写测试的时候@Test 标红 Cannot resolve symbol Test
原来Idea2017 只支持junit4以上版本 我依赖导入得是3.8.1版本
现在一般不会大小写 字母拼错了 但还有很多逻辑错误
今天写测试 没写方法一直报错 都忘记了 没方法运行不了
<!-- 注册userMapper.xml文件,
userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml-->
<mapper resource="user.xml"/>
<!-- 注册UserMapper映射接口-->
<mapper class="cn.bj.mybatis.model.UserMapperI"/>
这两个注册写错了 现在注释了
收获:使用spring的基础框架 和mybatis框架简单的融合
spring可以管理一切
因为一些代码看不懂 针对看了一些Java基础 像静态方法
用类的名称调用静态方法 以引用变量的名称调用静态方法
静态方法可以直接调用 而不需要实例
静态的方法不能调用非静态的变量 这就搞明白了一些看不懂的代码
都是静态方法的调用
构造函数 可以介入new的过程 当你需要通过用户的输入完成对象的创建的时候
你要自己编写构造方法 构造方法参数不同可以有很多种
评论