mybatis的简单使用

WBOY
发布: 2016-06-07 15:58:52
原创
1168 人浏览过

需要用到的包:(这里只是其中一个版本,其他的百度) mysql-connector-java-5.1.6-bin mybatis-3.2.2 先看项目目录: 配置文件mybatisconfig.xml: ?xml version=1.0 encoding=UTF-8?!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp:/

需要用到的包:(这里只是其中一个版本,其他的百度)

mysql-connector-java-5.1.6-bin

mybatis-3.2.2

先看项目目录:

\

配置文件mybatisconfig.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>
	<!-- 用XML映射的时候必须配此项!指定mappers里面的返回值对应的实体类 -->
	<!-- <typeAliases> 
        <typeAlias alias="User" type="com.miquan.mybatis.bean.User"/> 
    </typeAliases> -->
    <!-- JDBC配置 -->
	<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/weixinface"/>
				<property name="username" value="root"/>
				<property name="password" value=""/>
			</dataSource>
		</environment>
	</environments>
	<!-- mappers,每个实体类对应着一个xml -->
	<mappers>
		<mapper resource="com/miquan/mybatis/bean/User.xml"/>
	</mappers>
</configuration>
登录后复制
实体类User.java:
package com.miquan.mybatis.bean;

public class User {
	private int id;
	private String userName;
	private String password;
	
	public User(int id, String userName, String password) {
		super();
		this.id = id;
		this.userName = userName;
		this.password = password;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
登录后复制
User.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">
<!-- namespace对应着接口类 -->
<mapper namespace="com.miquan.mybatis.inter.IUserOperation">
	<!-- <select id="selectById" parameterType="int" resultType="User">
		select * from User where id = #{id}
	</select> -->
</mapper>
登录后复制
IUserOperation.java:
package com.miquan.mybatis.inter;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.miquan.mybatis.bean.User;

public interface IUserOperation {
	@Select("select * from User where id = #{id}")
	public User selectById(int id);
	
	@Select("select * from User where userName = #{userName}")
	public List<User> selectUsers(String userName);
	
	@Insert("insert into User values(null, #{userName}, #{password})")
	public boolean addUser(User user); 
	
	@Delete("delete from User where id = #{id}")
	public boolean delUser(int id);
	
	@Update("update User "
			+ "set userName = #{userName}, "
				+ "password = #{password} "
			+ "where id = #{id}")
	public boolean updateUser(User user);
}

最后进行测试:Test.java:
登录后复制
package com.miquan.mybatis.test;

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 com.miquan.mybatis.bean.User;
import com.miquan.mybatis.inter.IUserOperation;

public class Test {
	static SqlSession session;
	
	public static void main(String[] args) {
		try {
			//获取session
			InputStream is = Resources.getResourceAsStream("mybatisconfig.xml");
			SqlSessionFactory sqlSessionFactory = 
					new SqlSessionFactoryBuilder().build(is);
			session = sqlSessionFactory.openSession();
			
			//XML配置对应用法
//			User user = session.selectOne("com.miquan.mybatis.bean.UserDB.selectById", 1);
//			System.out.println(user.toString());
			
			//接口调用法
			//////注意:insert、delete和update方法调用之后必须commit才能真正的改变数据
			IUserOperation userOperation = session.getMapper(IUserOperation.class);
			
			//查
//			User user = userOperation.selectById(1);
//			System.out.println(user.toString());
			
			//查找List
//			List<User> users = userOperation.selectUsers("miquan");
//			System.out.println(users.size());
			
			//增
//			User user = new User();
//			user.setUserName("zhiquan");
//			user.setPassword("999");
//			userOperation.addUser(user);
//			session.commit();
			
			//删
//			userOperation.delUser(7);
//			session.commit();
			
			//改
			User user = new User(8, "qiantu", "shaxppp");
			userOperation.updateUser(user);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				session.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
}

简单的增删查改可以实现了。
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板