Mybatis入门(自己入门)

做j2ee开发得熟悉好多的框架呀,来来玩下MyBatis!


第一步、到github.com下载jar包;

第二步、把demo项目结构弄好:



第三步、Mybatis-Configuration.xml这个是Mybatis的配置文件,这里面好像是有很多参数可以配置。我就主要配置两个用途的:

a、数据库连接;

b、如何找到具体的sql配置文件。

<?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>
  <environments default="development">
	<environment id="development">
		<transactionManager type="JDBC"/>
		<dataSource type="POOLED">
			<property name="driver" value="com.mysql.jdbc.Driver"/>
			<property name="url" value="jdbc:mysql://localhost:3306/test"/>
			<property name="username" value="root"/>
			<property name="password" value="123456"/>
		</dataSource>
	</environment>
  </environments>
  <mappers>
  	<!-- import UserMapper.xml -->
	<mapper resource="com/cmpsky/mybatistest/data/UserMapper.xml"/>
  </mappers>
</configuration>


第四步、配置具体的sql配置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.cmpsky.mybatistest.dao.UserDao">
	<select id="getUser" parameterType="com.cmpsky.mybatistest.data.UserModel" resultType="com.cmpsky.mybatistest.data.UserModel">  
	    SELECT * FROM t_user_test WHERE username=#{username}
	</select>
	<insert id="addUser" parameterType="com.cmpsky.mybatistest.data.UserModel" flushCache="true">  
	   INSERT INTO t_user_test (username,sex,age) VALUES (#{username},#{sex},#{age})
	</insert>
	<update id="updateUser" parameterType="com.cmpsky.mybatistest.data.UserModel">  
	    UPDATE user SET age=#{age} WHERE username=#{username}
	</update>
	<delete id="deleteUser" parameterType="int">  
	    DELETE FROM user WHERE username=#{username} 
	</delete>
</mapper>

第五步、弄个pojo对象:


第六步、弄个Dao类:

import java.io.IOException;
import java.io.InputStream;

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 com.cmpsky.mybatistest.data.UserModel;

public class UserDao 
{
	public static SqlSessionFactory sqlSessionFactory;
	public static String resource = "com/cmpsky/mybatistest/data/Mybatis-Configuration.xml";
	public static InputStream inputStream;
	private SqlSession sqlSession;
	
	static
	{
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}
	
	public UserDao()
	{
		sqlSession = sqlSessionFactory.openSession();
	}
	
	public UserModel getUser(UserModel user)
	{
		return sqlSession.selectOne("com.cmpsky.mybatistest.dao.UserDao.getUser", user.getUsername());
	}
	
	public void insertUser(UserModel user)
	{
		sqlSession.insert("com.cmpsky.mybatistest.dao.UserDao.inserUser", user);
	}
	
	public void updateUser(UserModel user)
	{
		sqlSession.update("com.cmpsky.mybatistest.dao.UserDao.updateUser", user);
	}
	
	public void deleteUser(int id)
	{
		sqlSession.delete("com.cmpsky.mybatistest.dao.UserDao.deleteUser", id);
	}
}


完工!

过程比起自己写sql再处理结果集,还是简化了的


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值