1.数据库设计
2.项目结构(针对User不用管Blogger)
User.java
package com.yunqing.mybatis.bean; public class User { private Integer id; private String name; private String gender; private String email; //构造函数 public User() { } //带参构造函数 public User(Integer id, String name, String gender, String email) { this.id = id; this.name = name; this.gender = gender; this.email = email; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", gender='" + gender + '\'' + ", email='" + email + '\'' + '}'; } }
UserMapper.java接口类
package com.yunqing.mybatis.dao; import com.yunqing.mybatis.bean.User; import org.apache.ibatis.annotations.Select; import java.util.List; public interface UserMapper {
//这种写法可以用于简单的sql操作,复杂的请写在sql的xml映射文件中 @Select("select * from t_user") List<User> getAllUser(); User getUserById(Integer id);
//mybatis的增删改可以定义Integer|Long|boolean三种返回值类型 void insertUser(User user); void updateUser(User user); void deleteUserById(Integer id); }
UserMapper.xml(需要与接口类同名,批量注册到mybatis-config.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:命名空间,指定为接口的全类名 selectUserById:唯一标识 resultType:返回值类型 --> <mapper namespace="com.yunqing.mybatis.dao.UserMapper"> <select id="getUserById" resultType="com.yunqing.mybatis.bean.User"> select * from t_user where id = #{id} </select> <insert id="insertUser" parameterType="com.yunqing.mybatis.bean.User" useGeneratedKeys="true"> INSERT INTO t_user(name,gender,email) VALUES (#{name},#{gender},#{email}) </insert> <update id="updateUser" parameterType="com.yunqing.mybatis.bean.User"> UPDATE t_user SET name=#{name},gender=#{gender},email=#{email} where id=#{id} </update> <delete id="deleteUserById"> DELETE from t_user where id=#{id} </delete> </mapper>
UserMapperAnnotation.java介绍注解的方式(不用写sql的xml映射文件 )
package com.yunqing.mybatis.dao; import com.yunqing.mybatis.bean.User; import org.apache.ibatis.annotations.Select; public interface UserMapperAnnotation { @Select("SELECT * FROM t_user where id = #{id}") User getUserById(Integer id); }
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> <properties resource="conf/dbconfig.properties"></properties> <environments default="development"> <environment id="test"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///db_blog"/> <property name="username" value="root"/> <property name="password" value="5678"/> </dataSource> </environment> <environment id="development"> <transactionManager type="JDBC"/> <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> </environments> <!--将写好的sql映射文件注册到全局配置文件中,类路径下直接写 UserMapper.xml就行,要是在包中则写com/yunqing/.../UserMapper.xml--> <mappers> <!--<mapper resource="conf/UserMapper.xml"/>--> <!--<mapper class="com.yunqing.mybatis.dao.UserMapperAnnotation"/>--> <package name="com.yunqing.mybatis.dao"/> </mappers> </configuration>
dbconfig.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///mybatis
jdbc.username = root
jdbc.password = 5678
测试文件MybatisTest.java
package com.yunqing.mybatis.test; import com.yunqing.mybatis.bean.Blogger; import com.yunqing.mybatis.bean.User; import com.yunqing.mybatis.dao.BloggerMapper; import com.yunqing.mybatis.dao.UserMapper; import com.yunqing.mybatis.dao.UserMapperAnnotation; 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 java.io.IOException; import java.io.InputStream; import java.util.List; public class MybatisTest { @Test public void getAllUser() throws IOException { //从xml中获取sqlSessionFactory String resource = "conf/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //获取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); //之前的mybatis的写法 /*User user = sqlSession.selectOne("com.yunqing.mybatis.UserMapper.selectUserById",1); System.out.println(user);*/ //现在的mybatis接口式编程写法 //相当于接口的实现类 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.getAllUser();//查询第一条记录 System.out.println(list); sqlSession.close(); } @Test public void getUserById() throws IOException { //从xml中获取sqlSessionFactory String resource = "conf/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //获取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); /*//之前的mybatis的写法 User user = sqlSession.selectOne("com.yunqing.mybatis.UserMapper.selectUserById",1); System.out.println(user);*/ //现在的mybatis接口式编程写法 //相当于接口的实现类 UserMapperAnnotation userMapperAnnotation = sqlSession.getMapper(UserMapperAnnotation.class); User user = userMapperAnnotation.getUserById(1);//查询第一条记录 System.out.println(user); sqlSession.close(); } @Test public void insertUser() throws IOException { String resource = "conf/mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //使用自动提交,如果不使用则需要sqlSession.commit();手动提交 SqlSession sqlSession = sqlSessionFactory.openSession(true); //SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //需要写有参和无参构造函数 User user = new User(null,"Letme","1","letme@qq.com"); userMapper.insertUser(user); //sqlSession.commit(); } @Test public void updateUser() throws IOException { String res = "conf/mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(res); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(9,"uzi","1","uzi@qq.com"); userMapper.updateUser(user); } @Test public void deleteUserById() throws IOException { String res = "conf/mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(res); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.deleteUserById(9); } /*@Test public void test2() throws IOException { String resource = "conf/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); BloggerMapper bloggerMapper = sqlSession.getMapper(BloggerMapper.class); List<Blogger> list= bloggerMapper.getAllBloger(); System.out.println(list); sqlSession.close(); }*/ }
如果想获取自增主键的值需要在sql的映射xml文件中加上这两个参数