Mybatis入门(二)
昨天做的那个demo中通过xml文件里面配置的select语句进行数据库的操作,今天通过注解来进行同样的操作
工作目录:

主要是这个UserMapperI接口,这个接口不需要我们实现它,通过注解mybatis会动态帮我实现之,我们拿来用就OK了,是不是很贴心,^_^
具体看下这个接口:
package com.mld.Mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.mld.domain.User; public interface UserMapperI { //使用@Insert注解指明add方法要执行的SQL @Insert("insert into user(userId,userName,password,sex,email) values(#{userId},#{userName},#{password},#{sex},#{email})") public int add(User user); //使用@Delete注解指明deleteById方法要执行的SQL @Delete("delete from user where id=#{id}") public int deleteById(int id); //使用@Update注解指明update方法要执行的SQL @Update("update user set userName=#{userName},password=#{password},sex=#{sex},email=#{email} where userId=#{userId}") public int update(User user); //使用@Select注解指明getById方法要执行的SQL @Select("select * from user where userId=#{id}") public User getById(int id); //使用@Select注解指明getAll方法要执行的SQL @Select("select * form user") public List<User> getAll(); }
然后将这个接口注册到mybatis主配置文件中:
<?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> <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.0.0.1:3306/mydb" /> <property name="username" value="root" /> <property name="password" value="mald123" /> </dataSource> </environment> </environments> <mappers> <!-- 注册userMapper.xml文件 ,userMapper.xml位于com.mld.mapping这个包 下,所以resource写成com/mld/mapping/userMapping.xml --> <!-- <mapper resource="com/mld/mapping/userMapping.xml"></mapper> --> <mapper class="com.mld.Mapper.UserMapperI"></mapper> <!-- 此处若是class只能用.不能用/ --> </mappers> </configuration>
test.java看一下测试代码:
package com.mld.test; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.mld.Mapper.UserMapperI; import com.mld.domain.User; public class test { public static void main(String[] args) throws IOException{ String conf = "com/mld/test/conf.xml"; InputStream is = test.class.getClassLoader().getResourceAsStream(conf); SqlSessionFactory sessionfactory = new SqlSessionFactoryBuilder().build(is); SqlSession session = sessionfactory.openSession(true); //自动commit,默认是false,需要手动commit UserMapperI umi = session.getMapper(UserMapperI.class); //mybatis自动帮我们实现UserMapperI接口 User user = umi.getById(1); System.out.println(user); User ua = new User(); ua.setUserId("7"); ua.setEmail("aaa@123.com"); ua.setPassword("aaaa"); ua.setUserName("maple"); int a = umi.add(ua); session.commit(); //这个commit是我测试加的,不用管 System.out.println(umi.getById(10)); session.close(); //session用完需要关闭的 } }
不知道说啥了,就这吧,流程记录下来了,看着也清晰~~~啦啦啦

浙公网安备 33010602011771号