导jar包
结构
将老师发的xml文件复制到src中 添加以下代码
<environments default="developtment">
<environment id="developtment">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/zjgm?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zhongruan/dao/UserMapper1.xml"></mapper>
</mappers>
User见前篇
IUserDao
public interface IUserDao {
List<User> findAll();
void deleteById(int id);
void updateById(User user);
void addById(User user);
}
UserMapper
<mapper namespace="com.zhongruan.dao.IUserDao">
<select id="findAll" resultType="com.zhongruan.bean.User">
select * from tb_user
</select>
<delete id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</delete>
<update id="updateById" parameterType="com.zhongruan.bean.User">
update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
<update id="addById" parameterType="com.zhongruan.bean.User">
insert into tb_user (username,password) values (#{username},#{password})
</update>
</mapper>
Test
public class Test {
public static void main(String[] args) throws IOException {
Reader reader= Resources.getResourceAsReader("sqlMapConfig.xml");
SqlSessionFactory bulid=new SqlSessionFactoryBuilder().build(reader);
SqlSession session=bulid.openSession();
// List<User> users=session.selectList("findAll");
// System.out.println(users);//查
// session.delete("deleteById",8);//删
// User user=new User();
// user.setId(10);
// user.setUsername("abab");
// user.setPassword("123");
// session.update("updateById",user);//改
User user=new User();
user.setUsername("q");
user.setPassword("q");
session.insert("addById",user);//增
session.commit();
session.close();
}
}