使用Mybatis对MySql数据库进行操作实例
初学Mybatis,参考网络上的教程,自己做的个小练习,适合我这种刚入门的新手。
1.环境(数据库创建步骤省略)
MyEclipse 10 Mysql5.5
2.新建项目,导入包
mybatis-3.2.3.jar
mysql-connector-java-5.1.24-bin.jar
ibatis-2.3.0.677.jar
3.创建SqlMapConfig.xml文件
1 <?xml version="1.0" encoding= "UTF-8"?> 2 <!DOCTYPE sqlMapConfig 3 PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" 4 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd" > 5 6 <sqlMapConfig> 7 <settings useStatementNamespaces ="true" /> 8 <transactionManager type ="JDBC"> 9 <dataSource type ="SIMPLE"> 10 <property name ="JDBC.Driver" value= "com.mysql.jdbc.Driver" /> 11 <property name ="JDBC.ConnectionURL" value= "jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8" /> 12 <property name ="JDBC.Username" value="root" /> 13 <property name ="JDBC.Password" value="root" /> 14 </dataSource > 15 </transactionManager > 16 <sqlMap resource ="StudentMapper.xml" /> 17 18 </sqlMapConfig>
4.创建StudentMapper.xml文件
1 <?xml version="1.0" encoding= "UTF-8"?> 2 <!DOCTYPE sqlMap 3 PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 4 "http://ibatis.apache.org/dtd/sql-map-2.dtd" > 5 6 <sqlMap > 7 <typeAlias alias ="Student" type= "com.igaming.entity.Student" /> 8 <!--插入操作 --> 9 <insert id ="insert" parameterClass="Student"> 10 insert into Student(student_id, student_name, student_sex, 11 student_birthday, class_id) 12 values (#student_id#, #student_name#, 13 #student_sex#, #student_birthday#, 14 #class_id#) 15 <selectKey resultClass ="java.lang.String" keyProperty="student_id" > 16 select last_insert_id() as id 17 </selectKey > 18 </insert > 19 20 </sqlMap>
5.创建Student.java实例
1 public class Student { 2 private String student_id; 3 private String student_name; 4 private String student_sex; 5 private Date student_birthday; 6 private String class_id; 7 8 public Student() { } 9 10 public Student(String student_id, String student_name, String student_sex,Date student_birthday, String class_id) { 11 this. student_id = student_id; 12 this. student_name = student_name; 13 this. student_sex = student_sex; 14 this. student_birthday = student_birthday; 15 this. class_id = class_id; 16 } 17 // ..........(注:此处省略get,set方法) 18 }
6.编写测试类:
Demo.java
1 public class Demo { 2 static Reader rd = null; 3 static SqlMapClient smc = null; 4 /** 5 * @param args 6 * @throws IOException 7 * @throws SQLException 8 */ 9 public static void main(String[] args) throws IOException, SQLException { 10 rd = Resources.getResourceAsReader("SqlMapConfig.xml"); 11 smc = SqlMapClientBuilder.buildSqlMapClient(rd); 12 13 /* This would insert one record in Employee table. */ 14 System. out.println( "Going to insert record....."); 15 //新增 16 insetInfo(); 17 System. out.println( "Record Inserted Successfully "); 18 19 } 20 /** 21 * 新增 22 * @throws SQLException 23 */ 24 public static void insetInfo() throws SQLException{ 25 Student em = new Student( "10007", "张三", "男" , new Date(), "2"); 26 smc.insert( "insert", em); 27 } 28 }
运行测试结果:
数据库:
查询操作:
在StudentMapper.xml配置文件中添加如下代码:
1 <!-- 查询操作 --> 2 <select id = "getAll" resultClass= "Student"> 3 select * from Student 4 </select >
在Demo.java中编写查询方法:
1 /** 2 * 查询 3 * @throws SQLException 4 */ 5 public static void selectInfo() throws SQLException { 6 List<Student> listStudentInfo = (List<Student>) smc.queryForList("getAll", null); 7 for (Student s : listStudentInfo) { 8 System. out.println(s.getStudent_id()+ " "+s.getStudent_name()+" "+s.getStudent_sex()+ " "+s.getStudent_birthday()+" "+s.getClass_id()); 9 } 10 }
测试结果:
更新数据库数据:
在StudentMapper.xml配置文件中添加如下代码:
1 <!-- ibatis更新操作 --> 2 <update id ="updateById" parameterClass="java.lang.String"> 3 update Student set student_name=" Admol" where student_id=#student_id# 4 </update >
在Demo.java中编写测试方法:
1 /** 2 * 更新操作 3 * @throws SQLException 4 */ 5 public static void updateById() throws SQLException{ 6 smc.update( "updateById", "10002"); 7 }
测试结果如下:
删除操作:
在StudentMapper.xml配置文件中添加如下代码:
1 <!-- 删除操作 --> 2 <delete id ="deleteStudentByid" parameterClass="java.lang.String" > 3 delete from Student where student_id=#student_id# 4 </delete >
在Demo.java中编写测试方法:
1 /** 2 * 删除操作 3 * @throws SQLException 4 */ 5 public static void deleteStudentByid() throws SQLException{ 6 //删除 7 int i=smc.delete("deleteStudentByid", "10004"); 8 System.out.println("i="+i); 9 }
测试结果如下:
注意:
1.插入到数据库中乱码解决办法为:
jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8
2.StudentMapper.xml中增删改查的id属性必须和Demo中方法insert() query() delete() update()中的值对应。
项目整体结构图:
一个点赞,一个评论,既是肯定,又是鼓励!期待和你一起交流学习、共同进步! 微信搜索公众号“jinglingwangcoding”或扫描下方二维码,一起交流

浙公网安备 33010602011771号