Mybatis(二)入门程序-通过id查找用户、模糊查找用户、添加用户、删除用户
根据下图myBatis的架构,创建一个使用MyBatis的工程。
一、配置MyBatis 环境(如图)
1、sqlMapConfig.xml
首先,导入jar包(上图右边)并加载路径,然后在目录下创建一个xml文件 sqlMapConfig.xml , 并添加以下配置代码, 这就是上图架构中的第一步配置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"> <!-- 使用jdbc事务管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments>
2. 配置Mappern.xml
在src目录下创建一个包叫sqlmap, 用来放mappern.xml, 然后在在该包下面创建一个User.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:命名空间,用于隔离sql,还有一个很重要的作用,后面会讲 --> <mapper namespace="test">
//通过id查询用户 <select id="findUserById" parameterType ="Integer" resultType="mybatis.pojo.User"> select * from user where id = #{v} </select>
//模糊查询用户 <select id="findUserByUsername" parameterType ="String" resultType="mybatis.pojo.User"> select * from user where username like '%${value}%' </select>
//添加用户
<insert id="insertUser" parameterType ="mybatis.pojo.User">
insert into user (username,birthday,address,sex)
values(#{username},#{birthday},#{address},#{sex})
</insert>
// 更新用户信息
<update id="updateUserById" parameterType="mybatis.pojo.User">
update user
set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address}
where id = #{id}
</update>
//通过id删除用户
<delete id="deleteUserById" parameterType="Integer">
delete from user
where id = #{vvvvv}
</delete>
</mapper>
3. 然后在src目录下创建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
4、在src下创建mybatis.pojo包,并创建User类
package mybatis.pojo; import java.io.Serializable; import java.util.Date; public class User implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String username; private String sex; private Date birthday; private String address; public Integer getId() { return id; }
//......................................省略
二、 构造SqlSessionFactory、通过SqlSessionFactory创建sqlSession会话、通过sqlSession操作数据库
1、在src目录下创建测试包testpackage和测试文件TesMybatis 并输入以下package testpackageimport mybatis.pojo.User;
import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class TestMybatis { public static void main(String[] args) throws Exception { testMybatis(); } public static void testMybatis() throws Exception{
//配置mybatis环境配置信息
String resource = "sqlMapConfig.xml"; InputStream in =Resources.getResourceAsStream(resource);
//构造SqlSessionFactory(会话工厂)
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//通过SqlSessionFactory创建sqlSession会话
SqlSession sqlSession = sqlSessionFactory.openSession();
/*
(1)通过用户id查找用户
*/
User user = sqlSession.selectOne("test.findUserById", "o");
System.out.println(user);
/*
(2)模糊查询
*/ List<User> users = sqlSession.selectList("test.findUserByUsername", "J"); for(User user2: users){ System.out.println(user2); }
/*
(3)增加用户
*/
User user3 = new User();
user3.setUsername("Jane");
user3.setBirthday(new Date());
user3.setAddress("MountainView");
user3.setSex("1");
sqlSession.insert("test.insertUser", user3);
sqlSession.commit();
/*
(4)更新用户信息
*/
User user4 = new User();
user4.setId(10);
user4.setUsername("Luo");
user4.setBirthday(new Date());
user4.setAddress("21811");
user4.setSex("1");
sqlSession.update("test.updateUserById", user4);
sqlSession.commit();
/*
(5)删除用户信息
*/
sqlSession.delete("test.deleteUserById", 29);
sqlSession.commit();
}
}
赞赏码!!!!!!!!!!!!!!!