一、properties(属性)
可以引用java属性文件中的配置信息如下
jdbc.properties代码如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=beyond
sqlMapConfig.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"/><!-- 和spring整合后 environments配置将废除 --><environments default="development"><environment id="development"><!-- 使用jdbc事务管理 --><transactionManager type="JDBC" /><!-- 数据库连接池 --><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}" /><property name="url"value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /><property name="username" value="root" /><property name="password" value="beyond" /></dataSource></environment></environments><!-- Mapper的位置 --><mappers><mapper resource="sqlmap/User.xml"/></mappers></configuration>
MybatisMapperTest代码如下:
package com.pdsu.mybatis.mapper;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 org.junit.Test;import com.pdsu.mybatis.pojo.User;public class MybatisMapperTest {@Testpublic void testMapper() throws Exception {//加载核心配置文件,加载要使用IO流进行读取String resource = "sqlMapConfig.xml";InputStream in = Resources.getResourceAsStream(resource);//创建SqlSessionFactorySqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);//SqlSessionFactoryBuilder这是一个实现类需要new一下//创建SQLSessionSqlSession sqlSession = sqlSessionFactory.openSession(); //SqlSession自动生成实现类(接口需要遵循四大原则)=== UserMapper.class为遵循四大原则的接口UserMapper userMapper = sqlSession.getMapper(UserMapper.class);User user = userMapper.findUserById(30);System.out.println(user);}
}
UserMapper接口如下:
package com.pdsu.mybatis.mapper;
import java.util.List;
import com.pdsu.mybatis.pojo.User;
public interface UserMapper {/*遵循四个原则对于:public User findUserById(Integer id);接口 方法名 == User.xml中id名 === findUserById返回值类型 与 Mapper.xml文件中返回值类型(resultType)要一致 === User === com.pdsu.mybatis.pojo.User方法的入参类型 与 Mapper.xml中入参类型(parameterType)要一致 === Integer id命名空间绑定此接口<mapper namespace="com.pdsu.mybatis.mapper.UserMapper">,User.xml中的路径为该接口全路径*/public User findUserById(Integer id);}
User代码如下:
package com.pdsu.mybatis.pojo;import java.io.Serializable;
import java.util.Date;public class User implements Serializable {/*** */private static final long serialVersionUID = 1L;private Integer id;private String username;// 用户姓名private String sex;// 性别private Date birthday;// 生日private String address;// 地址public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", sex=" + sex+ ", birthday=" + birthday + ", address=" + address + "]";}
}
至于数据库创建,可以自己设计数据库也可以参考如下数据库:
https://github.com/beyondyanyu/Sayingyy/blob/master/JDBC2-数据库sql建表语句
二、typeAliases(别名)
SQLMapConfig.xml:
<typeAliases><!-- 别名<typeAlias type="com.pdsu.mybatis.pojo.User" alias="User"/> --><!-- 自动将com.pdsu.mybatis.pojo该包下的所有pojo,然后全部给配置给别名,不区分大小写 --><package name="com.pdsu.mybatis.pojo"/></typeAliases>
properties必须在typeAliases之前!!!!!顺序不可以颠倒
也就是说在User.xml里面
例如:<select id="findUserById" parameterType="Integer" resultType="user"> select * from user where id= #{sq} </select>
这个:通过ID查询一个用户
resultType可以为pojo包下的实体,并不用再具体到那个包下的那个pojo了
sqlMapConfig.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><!-- 可以引用java属性文件中的配置信息 --><properties resource="jdbc.properties"/><!-- 别名、包名若为包名的话,包和其子包下的所有类 头字母大小写都行 --><typeAliases><!-- 别名<typeAlias type="com.pdsu.mybatis.pojo.User" alias="User"/> --><!-- 自动将com.pdsu.mybatis.pojo该包下的所有pojo,然后全部给配置给别名,不区分大小写 --><package name="com.pdsu.mybatis.pojo"/></typeAliases><!-- properties必须在typeAliases之前!!!!!顺序不可以颠倒 --><!-- 和spring整合后 environments配置将废除 --><environments default="development"><environment id="development"><!-- 使用jdbc事务管理 --><transactionManager type="JDBC" /><!-- 数据库连接池 --><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}" /><property name="url"value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /><property name="username" value="root" /><property name="password" value="beyond" /></dataSource></environment></environments><!-- Mapper的位置 --><mappers><mapper resource="sqlmap/User.xml"/></mappers></configuration>
User.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">
<!-- namespace:命名空间,用于隔离sql,还有一个很重要的作用,后面会讲 -->
<!--
命名空间:user.findUserById
命名空间:order.findUserById
--><mapper namespace="com.pdsu.mybatis.mapper.UserMapper"><!-- resultType:返回值parameterType:输入参数 --><!-- 通过ID查询一个用户 --><select id="findUserById" parameterType="Integer" resultType="user">select * from user where id= #{sq}</select><!-- #{} === select * from user where username= 表示占位符? sq可以随意替代=== select * from user where username= '思琪'${} === select * from user where username like '%${value}%' 表示字符串拼接 value不可以随意替代=== select * from user where username like '%琪%' sql 模糊语句查询=== select * from user where username like "%"'琪%'"%" sql 模糊语句查询=== select * from user where username like "%"#{sq}"%" --><!-- 根据用户名称模糊查询用户列表 --><select id="findUserByUsername" parameterType="String" resultType="com.pdsu.mybatis.pojo.User">select * from user where username like '%${value}%'</select><!-- 添加用户 --><insert id="insertUser" parameterType="com.pdsu.mybatis.pojo.User"><selectKey keyProperty="id" resultType="Integer" order="AFTER" >select LAST_INSERT_ID()</selectKey>insert into user(username,birthday,address,sex) values (#{username},#{birthday},#{address},#{sex})</insert><!-- 更新用户 --><update id="updateUserById" parameterType="com.pdsu.mybatis.pojo.User">update user set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address}where id = #{id}</update> <!-- 删除用户 --><delete id="deleteUserById" parameterType="Integer" >delete from user where id = #{sq}</delete></mapper>