(一)问候MyBatis3

第一节:MyBatis简介

  百度百科

第二季:Mybatis版HolleWorld实现

  例子:

  (一)问候MyBatis3

mybatis-config.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>
<properties resource="jdbc.properties"/> <!-- 引入jdbc.properties文件 -->
<!-- 类型别名 -->
<typeAliases>
<typeAlias alias="Student" type="com.wishwzp.model.Student"/>
</typeAliases>
<!-- 开发环境,这里就写了一个id为development的数据环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="com/wishwzp/mappers/StudentMapper.xml" />
</mappers> </configuration>

jdbc.properties:

 jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

StudentTest.java:

 package com.wishwzp.service;

 import org.apache.ibatis.session.SqlSession;

 import com.wishwzp.mappers.StudentMapper;
import com.wishwzp.model.Student;
import com.wishwzp.util.SqlSessionFactoryUtil; public class StudentTest { public static void main(String[] args) {
SqlSession sqlSession=SqlSessionFactoryUtil.openSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
//do work
Student student=new Student("李四",11);
int result=studentMapper.add(student);
sqlSession.commit();
if(result>0){
System.out.println("添加成功!");
}
}
}

SqlSessionFactoryUtil.java:

 package com.wishwzp.util;

 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; public class SqlSessionFactoryUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory(){
if(sqlSessionFactory==null){
InputStream inputStream=null;
try{
inputStream=Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}catch(Exception e){
e.printStackTrace();
}
}
return sqlSessionFactory;
} public static SqlSession openSession(){
return getSqlSessionFactory().openSession();
}
}

Student.java:

 package com.wishwzp.model;

 public class Student {

     private Integer id;
private String name;
private Integer age; public Student() {
super();
// TODO Auto-generated constructor stub
} public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} }

StudentMapper.java:

 package com.wishwzp.mappers;

 import com.wishwzp.model.Student;

 public interface StudentMapper {

     public int add(Student student);
}

StudentMapper.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">
<mapper namespace="com.wishwzp.mappers.StudentMapper"> <insert id="add" parameterType="Student" >
insert into t_student values(null,#{name},#{age})
</insert> </mapper>

数据库为db_mybatis:

(一)问候MyBatis3

上一篇:【路由和交换之H3C自导自演】


下一篇:Redis基础认识及常用命令使用(一)--转载