本文介绍了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>
- <!--
- 拿到数据库信息,这是db.properties的内容
- driver=com.MySQL.jdbc.Driver
- url=jdbc:mysql://localhost:3306/usertest
- username=root
- password=root
- --!>
- <properties resource="db.properties">
- </properties>
- <!-- 别名优化--!>
- <typeAliases>
- <!-- <packagename="com.leige.entity"/> -->
- <typeAlias type="com.leige.entity.User"alias="user"/>
- </typeAliases>
- <!--设置框架工作模式development开发者模式 ,work是工作模式 -->
- <environmentsdefault="development">
- <environment id="development">
- <transactionManager type="jdbc"></transactionManager>
- <!-- 配置数据源 -->
- <dataSource type="POOLED">
- <!-- 注册驱动 -->
- <property name="driver"value="${driver}"/>
- <!-- 连接数据库 -->
- <property name="url"value="${url}"/>
- <!-- 密码帐号登录 -->
- <property name="username"value="${username}"/>
- <property name="password"value="${password}"/>
- </dataSource>
- </environment>
- </environments>
- <!-- 使用xml,mapper resource需要设置--!>
- <!-- <mappers>
- <mapper resource="com/leige/entity/UserMappen.xml"></mapper>
- </mappers> -->
- <!--使用注解,mapper配置class,查询方法写在接口中--!>
- <mappers>
- <mapperclass="com.leige.impl.UserImpl"/>
- </mappers>
复制代码
使用xml需要再配置一个mappen.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">
- <!-- 建立表与类的映射关系有两种:一个是xml(倾向),另一个是注解 -->
- <mapper namespace="com.leige.entity.UserMappen">
- <!-- 查询全部 -->
- <select id="selectAll"resultType="user">
- select * from user
- </select>
- <!-- 增加用户 -->
- <update id="insertUser"parameterType="user">
- insert into user values (null,#{name},#{password},#{age})
- </update>
- </mapper>
- 使用注解则用接口
- publicinterfaceUserImpl {
- //更新用户,更新用@update,查询用@select,插入用@insert,删除使用@delete这些注解
- @Update("update user set name=#{name} where id=#{id}")
- publicvoidupdateUser(User user);
- }
- demo测试代码
- publicclassDemo {
- staticInputStream in =null;
- staticSqlSessionFactory ssf =null;
- static{
- in = Demo.class.getClassLoader().getResourceAsStream("config.xml");
- ssf =newSqlSessionFactoryBuilder().build(in);
- }
- publicstaticvoidmain(String[] args) {
- //selectAll();
- //insertUser();
- updateUser();
- }
- //查询全部
- publicstaticvoidselectAll(){
- SqlSession session = ssf.openSession();
- List<User> list = session.selectList("com.leige.entity.UserMappen.selectAll");
- System.out.println(list);
- session.close();
- }
- //增加用户
- publicstaticvoidinsertUser(){
- SqlSession session = ssf.openSession();
- User user =newUser();
- user.setName("小霸王");
- user.setPassword("我是猪!a");
- user.setAge(25);
- intnum = session.insert("com.leige.entity.UserMappen.insertUser",user);
- session.commit();
- System.out.println(num);
- session.close();
- }
- //修改用户信息
- publicstaticvoidupdateUser(){
- SqlSession session = ssf.openSession();
- UserImpl userImpl = session.getMapper(UserImpl.class);
- User user =newUser();
- user.setName("我是大头鬼");
- user.setPassword("我是猪!a");
- user.setAge(25);
- user.setId(79);
- userImpl.updateUser(user);
- session.commit();
- session.close();
- }
- }
- </configuration>
复制代码 |