一、下载相关jar包
http://download.csdn.net/detail/eson_15/9542081
二、引入数据库驱动包
三、编写mybatis的核心配置文件mybatis.cfg.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> <!--
配置当前数据库环境(信息),default用来指定当前默认的数据库环境 --> <environments
default="development"> <!-- 具体的数据库执行环境 --> <environment id="development"> <!--
使用jdbc控制操作数据的事务 --> <transactionManager type="JDBC" /> <!-- 配置数据库连接池 -->
<dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property
name="url" value="jdbc:mysql://localhost:3306/dbuser"/> <property name="username" value="root" />
<property name="password" value="123456" /> </dataSource> </environment> </environments> <!--
加载mybait数据库操作的映射文件 --> <mappers> <mapper resource="com/sxt/mapper/User.xml"/>
</mappers> </configuration>
1
<?xml
version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE configuration
3
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5
<configuration>
6
<!-- 配置当前数据库环境(信息),default用来指定当前默认的数据库环境 -->
7
<environments default="development">
8
<!-- 具体的数据库执行环境 -->
9
<environment id="development">
10
<!-- 使用jdbc控制操作数据的事务 -->
11
<transactionManager type="JDBC" />
12
<!-- 配置数据库连接池 -->
13
<dataSource type="POOLED">
14
<property name="driver" value="com.mysql.jdbc.Driver" />
15
<property name="url" value="jdbc:mysql://localhost:3306/dbuser"/>
16
<property name="username" value="root" />
17
<property name="password" value="123456" />
18
</dataSource>
19
</environment>
20
</environments>
21
<!-- 加载mybait数据库操作的映射文件 -->
22
<mappers>
23
<mapper resource="com/sxt/mapper/User.xml"/>
24
</mappers>
25
</configuration>
四、创建数据库
五、创建实体类User
六、创建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.sxt.mapper.UserMapper">
<select id="addUser" parameterType="com.sxt.pojo.User"> insert into user values(null,#{uname},#{uage},#{upsw})
</select> </mapper>
七、创建UserDAO
八、建立UserDaoImpl实现类
public class UserDAOImpl implements UserDao {@Override public void addUser(User user) { //1
创建SqlSessionFactoryBuilder对象SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); //2
通过ssfb对象,构造SqlSessionFactory对象SqlSessionFactory ssf = ssfb.build(this.getClass().getClassLoader()
.getResourceAsStream("mybatis.cfg.xml")); //3 通过ssf创建SqlSession对象 SqlSession sqlSession =ssf.openSession();//4 数据库crud操作 /*** * arg0: namespace.id ,获得mapper.xml中的sql语句 * arg1: * **/
sqlSession.insert("com.sxt.mapper.UserMapper.addUser", user);//5 提交事务 sqlSession.commit(); } }
九、测试
@Test public void testMybatis(){UserDAO dao=new UserDAOImpl();
User user = new
User("张三",22,"123456");dao.addUser(user); }
十、Log4j的配置log4j.properties
# Global logging configuration log4j.rootLogger=DEBUG, stdout # MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE # 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