public class Test { public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); People people = ac.getBean("peo",People.class); System.out.println(people); String[] names = ac.getBeanDefinitionNames(); for (String string : names) { System.out.println(string); } } }
Spring创建对象的三种方式
1. 构造方法创建
无参构造创建:默认
有参构造创建:需要明确配置
2. 实例工厂
3. 静态工厂
构造方法创建(bean标签更改)
实体类创建好无参构造方法和有参构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public People(){ super(); System.out.println("执行无参构造方法"); }
public People(int id,String name){ super(); this.id=id; this.name=name; System.out.println("执行有参构造方法1"); }
public People(String name,int id){ super(); this.id=id; this.name=name; System.out.println("执行有参构造方法2"); }
<select id="selAll1" resultMap="mymap1"> select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t LEFT JOIN student s on t.id=s.tid; </select> </mapper>
//只列举了变量 private int id; private String name; private int age; private int tid; private Teacher teacher; //老师类的对象!!!!!!
2. teacher配置文件(一个是student)
1 2 3 4 5 6
<mapper namespace="com.bjsxt.mapper.TeacherMapper"> <select id="selAll" resultType="teacher"> select t.id id,t.name name,s.id `list.id`,s.name `list.name`,age `list.age`,tid `list.tid` from teacher t LEFT JOIN student s on t.id=s.tid //展示的是list集合输出 </select> </mapper>
3. student配置文件(里面通过老师对象(反单引号) teacher.id等传过来用)
1 2 3 4 5 6 7
<mapper namespace="com.bjsxt.mapper.StudentMapper"> <select id="selAll" resultType="student"> select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid from student s LEFT JOIN teacher t on t.id=s.tid //里面通过老师对象(反单引号) teacher.id等传过来用) </select> </mapper>
public class Test { public static void main(String[] args) { //使用ThreadLocal类 final ThreadLocal<String> threadLocal=new ThreadLocal<>(); //final不让实例化 threadLocal.set("测试"); //创建子线程 new Thread() { public void run() //匿名内部类 { String result = threadLocal.get(); System.out.println("结果"+result); }; }.start(); String result = threadLocal.get(); System.out.println(result); } }
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.AccountMapper"> <!-- 根据帐号和密码查询账户信息 --> <select id="selByAccnoPwd" resultType="account" parameterType="account"> //id就是此方法名称 resultType就是返回值类型 select * from account where accno=#{accNo} and password=#{password} </select>
<!-- 根据帐号和姓名查询账户信息 --> <select id="selByAccnoName" resultType="account" parameterType="account"> select * from account where accno=#{accNo} and name=#{name} </select>
<!-- 根据accNo修改账户余额 --> <update id="updBalanceByAccno" parameterType="account"> update account set balance=balance+#{balance} where accno=#{accNo} </update>
</mapper>
准备serive包下的AccountService接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public interface AccountService { //帐号和密码不匹配状态码 int ACCOUNT_PASSWORD_NOT_MATCH=1; //余额不足 int ACCOUNT_BALANCE_NOT_ENOUGH=2; //账户姓名不匹配 int ACCOUNT_NAME_NOT_MATCH=3; //转账失败 int ERROR=4; //转账成功 int SUCCESS=5; //转账功能 int transfer(Account accIn,Account accOut) throws IOException; }