一.基礎項目骨架的搭建
搭建方式比較靈活,不侷限於本人的搭建方式。jar包可能用的比較多,大家不着急實踐在文章後面會提供正個項目的源代碼,大家進行瀏覽操作即可。最後完成在數據庫中按照ID查詢user的信息的功能。最後是user在數據庫中的設計表。
2.項目文件以及思路解析
1.src目錄下創建dao,junit,mapper,pojo實現項目的骨架,大家關注junit(進行測試),mapper(實現動態代理),pojo(實類)即可。
pojo的實現
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;// 用戶姓名
private String sex;// 性別
private Date birthday;// 生日
private String address;// 地址
// 實現get&set&toString方法
}
usermapper接口的實現
import com.xupt.mybatis.pojo.User;
public interface UserMapper {
public User findUserById(Integer id);
}
關於junit的實現在最後進行實現
applicationContext.xml文件的實現
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<!--配置數據庫連接池dbcp數據源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!--mybatis工廠-->
<bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"/>
<!--mybatis核心配置文件-->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<!--Mapper動態代理開發-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
<property name="mapperInterface" value="com.xupt.mybatis.mapper.UserMapper"/>
</bean>
</beans>
db.propertis文件的實現
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=你的密碼
可能會感到奇怪,爲什麼url=jdbc:mysql://localhost/mybatis(數據庫名)而不是url=jdbc:mysql://localhost:端口號/mybatis
注意:如果使用後者的url方式,將會出現jdbc連接錯誤。
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection
因爲spring官網已經給出正確的db.properts的書寫方式,大家可以進行嘗試和查看官方文檔。
usermapper.xml文件的實現
<?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">
<mapper namespace="com.xupt.mybatis.mapper.UserMapper">
<!-- 通過ID查詢一個用戶 -->
<select id="findUserById" parameterType="Integer" resultType="User">
select * from user where id = #{v}
</select>
</mapper>
log4j.properts文件的實現
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
SqlMapConfig.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>
<!-- 設置別名 -->
<typeAliases>
<!-- 2. 指定掃描包,會把包內所有的類都設置別名,別名的名稱就是類名,大小寫不敏感 -->
<package name="com.xupt.mybatis.pojo" />
</typeAliases>
<mappers>
<package name="com.xupt.mybatis.mapper"/>
</mappers>
</configuration>
junit實現
import com.xupt.mybatis.mapper.UserMapper;
import com.xupt.mybatis.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TextDemo {
@Test
public void testMapper()throws Exception{
// 獲取spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 獲得spring容器中的userMapper
UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");
// 調用findUserById方法
User user = userMapper.findUserById(1);
// 打印ID=1的user的信息
System.out.println(user.toString());
}
}
結果:與數據庫信息對比
到此整個簡單實現就結束了。