1导包
2添加日志文件log4j.Properties
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
3编程步骤
1) 开发pojo类
2) 创建全局配置文件 SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <?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>
<!-- 加载配置文件 ${}获取key的值 -->
<properties resource="db.properties"/>
<typeAliases>
<!--默认的别名的名称是 类的类名的首字母大写或是小写 User user -->
<package name="com.domain"/>
</typeAliases>
<!-- 配置mybatis的环境信息 --> <environments default="development"> <environment id="development"> <!-- 配置JDBC事务控制,由mybatis进行管理 --> <transactionManager type="JDBC"></transactionManager> <!-- 配置数据源,采用dbcp连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb2?useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <--批量加载mapper文件 --> <mappers> <package name="com.mapper"/> </mappers> </configuration> </beans>
3) 编写mapper映射文件和mapper代理文件接口
4) 在全局配置文件中加载映射文件
5) 编写测试代码
a) 读取配置文件;
b) 通过SqlSessionFactoryBuilder创建SqlSessionFactory会话工厂。
c) 通过SqlSessionFactory创建SqlSession。
d) 调用SqlSession的操作数据库方法。
e) 关闭SqlSession。
public class Test1 { public static void main(String[] args) throws IOException { //获取全局配置文件对应的对象 InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); //创建全局对象 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); //开启会话 SqlSession session = factory.openSession(); //生成接口的代理对象来执行sql语句 userMapper代理对象 // userMapper代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); List<User> allUsers = userMapper.getAllUsers(); System.out.println(allUsers.get(0)); //关闭会话 session.close(); } }
4mapper的配置文件
<?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.mapper.UserMapper"> <select id="getAllUsers" resultType="user"> select user_name username from t_user </select> <!-- ? jdbc 占位符 #{}如果输入的参数是简单类型 名称任意 如果是pojo 取对应的java类的属性名 id 和接口的方法名一致 唯一的确定statementID parameterType 输入参数 执行sql语句需要的数据 要求对应的接口的方法的参数类型和它一致 java resultType sql语句执行的返回的结果的java类型 要求对应的接口的方法的返回的java类型一致 --> <select id="getUserByUid" parameterType="int" resultType="com.domain.User"> select * from t_user where uid = #{uid} </select> <!-- 根据用户的name模糊查询用户信息 ${} 不能防止sql注入,客户传递数据 取出用户的数据 , 如果是简单类型 value 用的不多 like order by #{} 使用的比较多 --> <select id="getUserLikeName" parameterType="String" resultType="com.domain.User"> select * from t_user where username like ‘%${value}%‘ </select> <!--根据用户名模糊查询和根据性别查询 --> <select id="getUserByUser" parameterType="com.domain.User" resultType="com.domain.User"> select * from t_user where username like ‘%${username}%‘ and gender = #{gender} </select> <delete id="deleteUserByUid" parameterType="int"> delete from t_user where uid = #{uid} </delete> <!-- 跟新用户信息 --> <update id="updateUserByUid" parameterType="com.domain.User"> UPDATE t_user SET username = #{username}, phone = #{phone} WHERE uid = #{uid} </update> <!-- 添加用户 --> <insert id="insertUser" parameterType="com.domain.User"> insert into t_user (uid , username , password,gender) values (null,#{username},#{password},#{gender}) </insert> <select id="getCountUser" resultType="int"> select count(*) from t_user </select> </mapper>
5在config配置db.properties文件
db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/big?useUnicode=true&characterEncoding=utf8 db.username=root db.password=1234