发表于: 2018-03-22 23:22:34
2 681
小白学习第9天,今天学习时间7-8小时,
今天完成的事情:(解决昨天的bug,在昨天一下午和今天一上午没有解决之后,我决定先跳过这个bug,对帮助我理解spring框架的代码进行深度理解,于是我决定在idea里面再写一边,就在开始写的时候,我发现了我昨天的bug,
我导jar包的代码是另一个FileSystemxml的导包,所以出现今天的问题
在解决之后运行了一遍代码
在解决了这些问题之后我决定继续对spring框架以及代码进行深度理解(昨天都是直接搬代码测试的)
于是我进行了改写如下
package service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
interface Dog{
void useMeat();
}
interface Meat{
String eat();
}
class Animal implements Dog {
private Meat meat;
public Animal() {}
public void setLanguage(Meat language) {this.meat = language;}
@Override
public void useMeat() {
System.out.println(meat.eat());
}
}
class Fat implements Meat{
@Override
public String eat() {
return "肥肉真好吃";
}
}
class Lean implements Meat{
@Override
public String eat() {
return "瘦肉更好吃";
}
}
class Cmt {
public static void main(String[] args) {
Animal animal = new Animal();
Fat fat = new Fat();
animal.setLanguage(fat);
animal.useMeat();
System.out.println("----------------------");
ApplicationContext cs = new ClassPathXmlApplicationContext("applicationContext.xml");
Dog p = (Dog) cs.getBean("cat");
p.useMeat();
}
}
代码中定义了Dog以及方法useMeat,接口Meat以及方法eat,Dog的实现类Animal,设值注入set,实现Dog接口的方法useMeat,Meat的第一个实现类Fat以及第二个实现类Lean,用spring容器来配置文件并实现注入,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.xsd">
<bean id="cat" class="service.Animal">
<property name="language" ref = "lean"></property>
</bean>
<bean id="fat" class="service.Fat"/>
<bean id="lean" class="service.Lean"/>
</beans>
定义了第一bean,以及被第一bean调用的 Fat bean 和Lean bean,调用两个bean,分别得出如下
以上结束了对spring的初步理解,spring管理bean与bean之间的依赖关系,采用set的方法来为目标bean注入属性,该方式称为设值注入。
package jdbc接口;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
public class BDHelper {
public static void main(String[] args) throws Exception {
Connection conn = null;
String sql;
String url = "jdbc:mysql://localhost:3306/zyy?"
+ "user=root&password=8520963.&useUnicode=true&characterEncoding=UTF8";
try {
Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动
// or:
// com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
// or:
// new com.mysql.jdbc.Driver();
System.out.println("成功加载MySQL驱动程序");
conn = DriverManager.getConnection(url);
// Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等
Statement stmt = conn.createStatement();
sql = "select name,QQ from sign_up_table";
System.out.println("查询数据表成功");
ResultSet rs = stmt.executeQuery(sql);
System.out.println("姓名\tQQ");
while(rs.next()){
System.out.println(rs.getObject(1) + "\t" + rs.getObject(2));// 入如果返回的是int类型可以用getInt()
}
} catch (SQLException e) {
System.out.println("MySQL操作错误");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
}
}


进行了初步的编写
代码没有完成,明天继续,)

遇到的问题:(有什么不好的地方不全面的地方希望师兄多指导,学习方法方向什么的

收获:(深度理解了依赖,对spring大致的功能有个概念了,对jdbc的进一步了解,学习了Mybatis)
最后感谢师兄的帮助
评论