mybatis的总结

本文详细介绍了使用MyBatis框架进行数据库操作的入门教程,包括开发环境搭建、配置文件解析、映射文件设置及基本的CRUD操作示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis入门(一)

开发环境 idea+maven+jdk1.8

  1. 三个配置文件,mybatis3.4 , mysql-connector-java, log4j 1.27
<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.47</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/log4j/log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

2.三个配置:总的配置文件config.xml和映射文件student.xml还有pojo

config.xml

<configuration>
    <environments default="MySQL">
       <environment id="MySQL">
           <transactionManager type="JDBC"></transactionManager>
           <dataSource type="POOLED">
                <property name="username" value="root"/>
                <property name="password" value="123"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/s6x"/>
                <property name="driver" value="com.mysql.jdbc.Driver"/>
           </dataSource>
       </environment>
    </environments>
    //此处填写映射的配置文件
    <mappers>
        <mapper resource="com/pojo/student.xml"/>
    </mappers>
</configuration>  

student.xml

<mapper namespace="com.dao.StudentDAO">
      <!-- 
          public List<Student> findAll();查询所有的学生
       -->
      <select id="findAll" resultType="com.pojo.Student">
           select xh,xm,cs,money from student
      </select>
      <!-- 
          public Student findById(int xh); 根据id查找
          #{} ognl表达式
          ${} el表达式
       -->
      <select id="findById" parameterType="int" resultType="com.pojo.Student">
          select xh,xm,cs,money from student 
               where xh=#{value}
      </select> 
       <!-- 
          public Student findById(int xh);
          #{} ognl表达式
          ${} el表达式
       -->
      <select id="findById2" parameterType="int" resultType="com.pojo.Student">
          select xh,xm,cs,money from student 
               where xh=${value}
      </select> 
</mapper>

student

public class Student implements java.io.Serializable {

	private int xh;
	private String xm;
	private Date cs;
	private float money;

	public int getXh() {
		return xh;
	}

	public void setXh(int xh) {
		this.xh = xh;
	}

	public String getXm() {
		return xm;
	}

	public void setXm(String xm) {
		this.xm = xm;
	}

	public Date getCs() {
		return cs;
	}

	public void setCs(Date cs) {
		this.cs = cs;
	}

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

}

再做一个测试

public class 查所有学生 {

	public static void main(String[] args) throws IOException {
		// 1.读取总的配置文件
		Reader reader = Resources.getResourceAsReader("configxml.xml");

		//2.创建session工厂
		SqlSessionFactory  factory = new SqlSessionFactoryBuilder().build(reader);
		
		//3.打开session
		SqlSession session = factory.openSession();
		
		//4.增删改查
		List<Student> list = session.selectList("com.dao.StudentDAO.findAll");
	    for (Student s : list) {
			System.out.println(s.getXh()+"\t"+s.getXm());
		}
	    //5.提交事务
	    session.commit();
	    
	    //6.关闭session
	    session.close();
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值