spring-mybatis框架 demo
package com.mybatis.dao;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
public class testmybatis {
public static void main() {
insert();
}
private static void insert() {
//插入语句
SqlSessionFactory sqlSessionFactory=userUtil.getSqlSessionFactory();
SqlSession session = sqlSessionFactory.openSession();
userMapper userInterface=session.getMapper(userMapper.class);
try {
User user=new User();
user.setUsername("zhangsan");
user.setPassword("123");
user.setEmail("476116612@qq.com");
user.setPhonenum("18696305976");
user.setSex("男");
userInterface.insert(user);
} finally {
session.commit();
session.close();
}
}
}
创建javabean User 对象
package com.mybatis.dao;
public class User {
private int id;
private String username;
private String password;
private String phonenum;
private String email;
private String sex;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public User(String name,String password,String phonenum,String sex,String email){
super();
this.username=name;
this.password=password;
this.phonenum=phonenum;
this.sex=sex;
this.email=email;
}
public User(){
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
mappper 配置SQL 标签
<?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="User">
<resultMap id="result" type="user">
<result property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<select id="selectAll" resultMap="result">
SELECT * FROM user;
</select>
<insert id="insert" parameterType="user" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user (username,phonenum,email,sex,password) VALUES (#{username},#{phonenum},#{email},#{sex},#{password});
</insert>
<update id="update" parameterType="com.mybatis.dao.User">
UPDATE user
SET name = #{name}
WHERE id = #{id}
</update>
<delete id="delete" parameterType="int">
DELETE from user WHERE id = #{id}
</delete>
</mapper>
自定义接口类 interface
package com.mybatis.dao;
public interface userMapper {
public void selectAll();
public void insert(User user);
public void update();
public void delete();
}
配置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>
<settings>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<typeAlias alias="User" type="com.mybatis.dao.User"/>
</typeAliases>
<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://127.16.19.107:3306/jessica?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="jessica"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="userMapper.xml" />
</mappers>
</configuration>
创建sqlsessionFactory
package com.mybatis.dao;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class userUtil {
private final static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
System.out.println(e.getMessage());
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}