一.基础项目骨架的搭建
搭建方式比较灵活,不局限于本人的搭建方式。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());
}
}
结果:与数据库信息对比
到此整个简单实现就结束了。