mybatis框架insert入门案例(1)

it2024-12-29  10

1.首先导入jar包 2.配置mybatis.xml 3.映射sql语句 第一个mybatis.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> <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:///class1" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="StudentMapper.xml" /> </mappers> </configuration> log4j.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <param name="Encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" /> </layout> </appender> <logger name="java.sql"> <level value="debug" /> </logger> <logger name="org.apache.ibatis"> <level value="info" /> </logger> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration> 映射sql 编写接口StudentMapper package com.gg.dao; import com.gg.entity.Student; public interface StudentMapper { public void insertStudent(Student student); } <?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.gg.dao.StudentMapper"> <!-- id:該抽象方法的名稱 --> <insert id="insertStudent"> <!--#{}相当于占位符 --> insert into student values (#{sid},#{sname},#{sage}); </insert> </mapper>

实体类

package com.gg.entity; public class Student { private String sid; private String sname; private Integer sage; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String sid, String sname, Integer sage) { super(); this.sid = sid; this.sname = sname; this.sage = sage; } @Override public String toString() { return "Student [sid=" + sid + ", sname=" + sname + ", sage=" + sage + "]"; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Integer getSage() { return sage; } public void setSage(Integer sage) { this.sage = sage; } }

测试类‘

package com.gg.demo; 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.gg.dao.StudentMapper; import com.gg.entity.Student; public class Demo { public static void main(String[] args) throws Exception { //定義主配置文件的相對路徑 String resource = "mybatis.xml"; //獲取当前主配置文件的输入流 InputStream inputStream = Resources.getResourceAsStream(resource); //创建sqlSessionFactory工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //session sqlsession SqlSession session=sqlSessionFactory.openSession(false); StudentMapper mapper=session.getMapper(StudentMapper.class); Student student=new Student("1012", "sname", 12); mapper.insertStudent(student); session.commit(); session.close(); } }
最新回复(0)