程序猿全敏的gravatar头像
程序猿全敏2016-07-12 09:53:01

Mybatis入门教程之增删查实例

三大框架入门都讲了,现在讲一个轻量级的框架Mybatis。

首先,要承认的是这个框架和Hibernate是类似的,更灵活,更容易学。

1.新建一个项目,加上Mybatis的核心包和数据库连接包

2.新建一个实体User,加上get,set方法

package com.qm.entity;

public class User {
	private int id;
	private String name;
	private String pwd;
	
	public User() {
	}
	
	public User(String name, String pwd) {
		this.name = name;
		this.pwd = pwd;
	}
	
	public User(int id, String name, String pwd) {
		super();
		this.id = id;
		this.name = name;
		this.pwd = pwd;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getPwd() {
		return pwd;
	}
	
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
	}
}

3.新建一个配置文件conf.xml用来连接数据库

    <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/ssh" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>

4.新建一个映射文件。userMapper.xml 里面可以直接写sql,非常灵活

<mapper namespace="com.qm.mapping.userMapper">

5.在配置文件注册

<mappers>
         <!-- 注册userMapper.xml文件-->
         <mapper resource="com/qm/mapping/userMapper.xml"/>
     </mappers>

6.新建一个测试类开始测试,这是添加

	        String resource = "conf.xml";
	        Reader reader;
			try {
				reader = Resources.getResourceAsReader(resource);
				//构建sqlSession的工厂
				SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
		                SqlSession session = sessionFactory.openSession();
		               String statement="com.qm.mapping.userMapper.insertUser";
		               User user1=new User("quanmin","888");
		               session.insert(statement, user1);
		               session.commit();
			} catch (IOException e) {
				e.printStackTrace();
			} 

7.在映射文件写上对应的sql语句

运行结果

添加:刚刚添加的quanmin,看数据库

Mybatis入门教程之增删查实例

删除:删除刚刚添加的数据

Mybatis入门教程之增删查实例

通过id查找

Mybatis入门教程之增删查实例

查找所有:

Mybatis入门教程之增删查实例


打赏

文件名:mybiats入门资料.rar,文件大小:986.119K下载
  • /
    • /ssh.sql
      • /testm
          • /testm/testm
            • /testm/testm/.classpath
            • /testm/testm/.mymetadata
            • /testm/testm/.project
              • /testm/testm/.settings
                • /testm/testm/.settings/.jsdtscope
                • /testm/testm/.settings/org.eclipse.jdt.core.prefs
                • /testm/testm/.settings/org.eclipse.wst.jsdt.ui.superType.container
                • /testm/testm/.settings/org.eclipse.wst.jsdt.ui.superType.name
              • /testm/testm/src
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友