首先mybatis有一个总的配置文件,和映射文件,可以在下载的pdf介绍中参考写法
下面演示一个使用mybatis的CUDR操作
总配置文件sqlMapConfig.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> <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:///mybatis" /> <property name="username" value="root" /> <property name="password" value="123" /> </dataSource> </environment> </environments> <mappers> <!-- 映射文件 --> <mapper resource="cn/mybatis/domain/User.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"> <mapper namespace="cn.mybatis.domain.User"><!-- namespace保证唯一 --> <!-- 根据id查询, parameterType参数类型, resultType结果类型--> <select id="selectUserById" parameterType="string" resultType="cn.mybatis.domain.User"> select * from User where id = #{id} </select> <!-- 查询返回为map --> <select id="selectUserByIdForMap" parameterType="string" resultType="hashmap"> select * from User where id = #{id} </select> <!-- 动态sql where可以代替1=1--> <select id="selectUserByCondition" parameterType="cn.mybatis.domain.User" resultType="cn.mybatis.domain.User"> select * from User where <where> <if test="id != null"> id = #{id} </if> <if test="name != null"> and name = #{name} </if> <if test="address != null"> and address = #{address} </if> </where> </select> <!-- 查询所有 --> <select id="selectAllUser" resultType="cn.mybatis.domain.User"> select * from User </select> <!-- 插入 --> <select id="insertUser" parameterType="cn.mybatis.domain.User"> insert into user (id, name, address) values (#{id}, #{name}, #{address}) </select> <!-- 删除 --> <select id="deleteUserById" parameterType="string"> delete from user where id = #{id} </select> <!-- 更新--> <select id="updateUserById" parameterType="cn.mybatis.domain.User"> update user set name = #{name}, address = #{address} where id = #{id} </select> </mapper>
package cn.mybatis.test; import java.io.InputStream; import java.util.List; import java.util.Map; 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 cn.mybatis.domain.User; public class MybatisTest { public SqlSession getSession() throws Exception { String resource = "sqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = ssf.openSession(); return session; } /** * 查询User * @throws Exception */ @Test public void selectOne() throws Exception { String resource = "sqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = ssf.openSession(); User user = session.selectOne("cn.mybatis.domain.User.selectUserById", "001"); System.out.println(user); } /** * 查询返回map类型 * @throws Exception */ @Test public void selectUserForMap() throws Exception { SqlSession session = getSession(); Map<String, String> map = session.selectOne("cn.mybatis.domain.User.selectUserByIdForMap", "001"); System.out.println(map); } //删除 @Test public void deleteUserById() throws Exception { SqlSession session = getSession(); session.delete("cn.mybatis.domain.User.deleteUserById", "002"); session.commit(); } //更新 @Test public void updateUserById() throws Exception { SqlSession session = getSession(); User user = new User(); user.setId("002"); user.setName("zhaodaxia"); user.setAddress("shanghai"); session.delete("cn.mybatis.domain.User.updateUserById", user); session.commit(); } //查询所有 @Test public void selectList() throws Exception { String resource = "sqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = ssf.openSession(); List<User> userList = session.selectList("cn.mybatis.domain.User.selectAllUser"); for (User user : userList) { System.out.println(user); } } //插入 @Test public void insertUser() throws Exception { SqlSession session = getSession(); User user = new User(); user.setId("002"); user.setName("xiaozhao"); user.setAddress("shanghai"); //i影响的数据库条数 int i = session.insert("cn.mybatis.domain.User.insertUser", user); //影响数据库需要提交事务 session.commit(); System.out.println(i); } //动态sql @Test public void selectUserByCondition() throws Exception { SqlSession session = getSession(); User user = new User(); user.setName("zhaodaxia"); List<User> userList = session.selectList("cn.mybatis.domain.User.selectUserByCondition", user); for (User userResult : userList) { System.out.println(userResult); } } }