相關jar(其中有些不必須有,由於是從另外項目複製過來的,懶得刪):
pojo:
package com.bjsxt.pojo;
public class Flower {
private int id;
private String name;
private double price;
private String production;
public Flower(int id, String name, double price, String production) {
super();
this.id = id;
this.name = name;
this.price = price;
this.production = production;
}
public Flower() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getProduction() {
return production;
}
public void setProduction(String production) {
this.production = production;
}
@Override
public String toString() {
return "Flower [id=" + id + ", name=" + name + ", price=" + price + ", production=" + production + "]";
}
}
編寫工具類,獲得bean對象。
package com.tool;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppContextCreate {
private static ApplicationContext appContext;
static{
appContext=new ClassPathXmlApplicationContext("applicationContext.xml");
}
public static <T>T getBean(Class<T> t){
return appContext.getBean(t);
}
}
mapper:
package mapper;
import java.util.List;
import com.bjsxt.pojo.Flower;
public interface FlowerMapper {
public List<Flower> listFlower();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namesapce:理解成實現類的全路徑(包名+類名) -->
<mapper namespace="mapper.FlowerMapper" >
<cache readOnly="true"></cache>
<select id="listFlower" resultType="com.bjsxt.pojo.Flower" >
select * from flower
</select>
</mapper>
applicationContext:
<?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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- spring幫助創建sqlsession -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- sping創建掃描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
</beans>
logo4j.propeties:
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
#log4j.com.bjsxt=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# if you use pattern layout ,you must use ConversionPattern to format data
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
Test:
package com.bjsxt.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.pojo.Flower;
import com.tool.AppContextCreate;
import mapper.FlowerMapper;
import sun.awt.AppContext;
public class Test {
public static void main(String[] args) throws IOException {
FlowerMapper bean = AppContextCreate.getBean(FlowerMapper.class);
List<Flower> listFlower = bean.listFlower();
for (Flower flower : listFlower) {
System.out.println(flower);
}
}
}
結果(數據內容不必太在意):
如果只是普通的java項目,是不需要web.xml的配置也是能完成的。如果是web項目,那麼需要在web.xml中進行sping的配置。不妥之處,請賜教。