源码下载:https://git.oschina.net/zhuhongliang/Spring4.0.2_Mybatis3.2.0_J2SE_Study
文档目录结构:(不算log4j的话一共就6个文件)
数据库:
下面看代码:
app.java:
package com.zhl.main; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Service; import com.zhl.service.FavoriteService; @Service public class app { @Autowired FavoriteService favorite; public void setFavorite(FavoriteService favorite){ this.favorite = favorite; } public static void main(String[] args){ long aa=System.currentTimeMillis(); @SuppressWarnings("resource") //加载配置文件耗时将近一秒,如果ApplicationContext.xml的9、11、13行最后的spring版本写错了会导致这里加载慢3-30倍 ApplicationContext context2 = new ClassPathXmlApplicationContext("ApplicationContext.xml"); long bb=System.currentTimeMillis(); //方法1:明显慢一点,差不多0.06秒 app qqq = (app) context2.getBean("app"); qqq.favorite.findAll(); long cc=System.currentTimeMillis(); //方法2:明显快一些,差不多0.01秒,相差大概6-7倍的速度 FavoriteService a = (FavoriteService) context2.getBean("favoriteService"); a.findAll(); long dd=System.currentTimeMillis(); System.out.println("运行时间:"+(bb-aa)/1000f+"\t"+(cc-bb)/1000f+"\t"+(dd-cc)/1000f); } }
FavoriteMapper.java:
package com.zhl.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.zhl.model.Favorite; public interface FavoriteMapper { @Insert("insert into `favorite` (station, station_code) values (#{station}, #{station_code})") int insert(Favorite favorite); @Select("select * from `favorite`") List findAll(); @Select("select * from `favorite` where id = #{id}") Favorite findOne( String id); @Update("Update `favorite` set station = #{station}, station_code = #{station_code} where id = #{id}") int update(Favorite favorite); @Delete("delete from `favorite` where id = #{id}") int delete(String id); }
Favorite.java:
package com.zhl.model; public class Favorite { public Favorite(){ super(); } private String id; private String station; //火车车次 private String station_code; //火车长码 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getStation() { return station; } public void setStation(String station) { this.station = station; } public String getStation_code() { return station_code; } public void setStation_code(String station_code) { this.station_code = station_code; } }
FavoriteService.java:
package com.zhl.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.zhl.mapper.FavoriteMapper; import com.zhl.model.Favorite; @Service public class FavoriteService { @Autowired private FavoriteMapper favoriteMapper; public void setFavoriteMapper(FavoriteMapper favoriteMapper) { this.favoriteMapper = favoriteMapper; } public void findAll(){ List allUsers = favoriteMapper.findAll(); for(int i = 0; i < allUsers.size(); i++){ System.out.println(allUsers.get(i).getId()+"\t"+allUsers.get(i).getStation()+"\t"+allUsers.get(i).getStation_code()); } } }
ApplicationContext.xml:(第9行、11行、13行,这三行后面对应spring的数字不要写错了,我开始写成了4.2结果导致app.java加载xml超级慢)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.gjt.mm.mysql.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/hitu_crm?useUnicode=true&characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <!-- 下面是自动扫描 --> <context:component-scan base-package="com.zhl.service"/> <context:component-scan base-package="com.zhl.main"/> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zhl.mapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jdbcDataSource" /> <property name="configLocation" value="classpath:mybatis.xml"></property> </bean> </beans>
log4j的配置文件就不贴出来了,无关紧要的东西
mybatis.xml:(这个文件里真的就是这么写的,什么都没写)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
运行结果:
1 北京 BJP 2 上海 SHH 3 天津 TJP 4 重庆 CQW 5 长沙 CSQ 6 长春 CCT 7 成都 CDW 8 福州 FZS 9 广州 GZQ 10 贵阳 GIW 11 呼和浩特 HHC 12 哈尔滨 HBB 13 合肥 HFH 14 杭州 HZH 15 海口 VUQ 16 济南 JNK 17 昆明 KMM 18 拉萨 LSO 19 兰州 LZJ 20 南宁 NNZ 21 南京 NJH 22 南昌 NCG 23 沈阳 SYT 24 石家庄 SJP 25 太原 TYV 26 乌鲁木齐南 WMR 27 武汉 WHN 28 西宁 XNO 29 西安 XAY 30 银川 YIJ 31 郑州 ZZF 32 深圳 SZQ 33 厦门 XMS 35 嘿嘿嘿 哈哈哈 36 33333333333333333333 2222222222 1 北京 BJP 2 上海 SHH 3 天津 TJP 4 重庆 CQW 5 长沙 CSQ 6 长春 CCT 7 成都 CDW 8 福州 FZS 9 广州 GZQ 10 贵阳 GIW 11 呼和浩特 HHC 12 哈尔滨 HBB 13 合肥 HFH 14 杭州 HZH 15 海口 VUQ 16 济南 JNK 17 昆明 KMM 18 拉萨 LSO 19 兰州 LZJ 20 南宁 NNZ 21 南京 NJH 22 南昌 NCG 23 沈阳 SYT 24 石家庄 SJP 25 太原 TYV 26 乌鲁木齐南 WMR 27 武汉 WHN 28 西宁 XNO 29 西安 XAY 30 银川 YIJ 31 郑州 ZZF 32 深圳 SZQ 33 厦门 XMS 35 嘿嘿嘿 哈哈哈 36 33333333333333333333 2222222222 运行时间:0.975 0.061 0.009
源码下载:https://git.oschina.net/zhuhongliang/Spring4.0.2_Mybatis3.2.0_J2SE_Study