<?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> <property name="jdbc.driver" value="com.mysql.jdbc.Driver"/> </properties> --> <!-- <properties>:设置或引入资源文件 resource:在类路径下访问资源文件 url:在网络路径或磁盘路径下访问资源文件 --> <properties resource="jdbc.properties"></properties> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <typeAliases> <!-- 为类型设置类型别名 type:Java 类型,若只设置type,默认的别名就是类型,且不区分大小写 --> <!-- <typeAlias type="com.atguigu.bean.User" alias="u"/> 不建议--> <!-- 这种写法在XxxMapper.xml的resultType只需要写个Xxx就可以 --> <package name="com.atguigu.bean"/> </typeAliases> <!-- <environments>:设置连接数据库的环境 default:设置默认使用的数据库环境 --> <environments default="mysql"> <!-- <environment>:设置某个具体的数据库的环境 id:数据库环境的唯一标示 --> <environment id="mysql"> <!-- type="JDBC|MANAGED" --> <transactionManager type="JDBC" /> <!-- type="POOLED|UNPOOLED|JNDI" --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> <environment id="oracle"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/ssm" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <!-- 引入映射文件 --> <mappers> <!-- 引入具体的映射文件 --> <mapper resource="UserMapper.xml" /> </mappers> </configuration>
<?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"> <!-- 这个用来指定对应的dao接口 --> <mapper namespace="com.atguigu.mapper.UserMapper"> <!-- <select>:定义查询语句 id:设置SQL语句的唯一标示 resultType:结果类型,即实体类的全限定名 resultType如果在sqlMapConfig.xml中配置过typeAlisas,则按起的别名来,不建议 --> <select id="getUserByUid" resultType="com.atguigu.bean.User"> select uid,username,password,age,sex from user where uid = ${value} </select> </mapper>
package com.atguigu.mapper; import com.atguigu.bean.User; public interface UserMapper { //和UserMapper.xml中的sql的id对应的 /** * <!-- <select>:定义查询语句 id:设置SQL语句的唯一标示 resultType:结果类型,即实体类的全限定名 --> <select id="getUserByUid" resultType="com.atguigu.bean.User"> select uid,username,password,age,sex from user where uid = ${value} 或者#{id} </select> * @param uid * @return */ User getUserByUid(String uid); }
package com.atguigu.mapper; import static org.junit.Assert.*; import java.io.IOException; import java.io.InputStream; import java.util.Properties; 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.atguigu.bean.User; public class TestMybatis { @Test public void test() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml");//org.apache.ibatis.io.Resources //InputStream is = TestMybatis.class.getClassLoader().getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(); //getMapper():会通过动态代理动态生成UserMapper的代理实现类 UserMapper mapper = sqlSession.getMapper(UserMapper.class); System.out.println(mapper.getClass().getName());//com.sun.proxy.$Proxy4这个为生成的代理对象 User user = mapper.getUserByUid("1"); System.out.println(user); //user_name ---> userName } }