Heim > Datenbank > MySQL-Tutorial > mybatis的简单使用

mybatis的简单使用

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-06-07 15:58:52
Original
1200 Leute haben es durchsucht

需要用到的包:(这里只是其中一个版本,其他的百度) 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>
Nach dem Login kopieren
实体类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;
	}
}
Nach dem Login kopieren
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>
Nach dem Login kopieren
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:
Nach dem Login kopieren
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();
			}
        }
	}
}

简单的增删查改可以实现了。
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Aktuelle Ausgaben
Mybatis-Zuordnungsproblem
Aus 1970-01-01 08:00:00
0
0
0
REGEXP in Mybatis
Aus 1970-01-01 08:00:00
0
0
0
java – Mybatis-bezogene Abfrage
Aus 1970-01-01 08:00:00
0
0
0
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage