Mybatis开发环境搭建实现数据的增删改查功能
程序员文章站
2024-03-06 08:23:07
config.xml的配置
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> <!-- 拿到数据库信息,这是db.properties的内容 driver=com.mysql.jdbc.driver url=jdbc:mysql://localhost:3306/usertest username=root password=root --!> <properties resource="db.properties"> </properties> <!-- 别名优化--!> <typealiases> <!-- <package name="com.leige.entity" /> --> <typealias type="com.leige.entity.user" alias="user"/> </typealiases> <!--设置框架工作模式development开发者模式 ,work是工作模式 --> <environments default="development"> <environment id="development"> <transactionmanager type="jdbc"></transactionmanager> <!-- 配置数据源 --> <datasource type="pooled"> <!-- 注册驱动 --> <property name="driver" value="${driver}"/> <!-- 连接数据库 --> <property name="url" value="${url}"/> <!-- 密码帐号登录 --> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </datasource> </environment> </environments> <!-- 使用xml,mapper resource需要设置--!> <!-- <mappers> <mapper resource="com/leige/entity/usermappen.xml"></mapper> </mappers> --> <!--使用注解,mapper配置class,查询方法写在接口中--!> <mappers> <mapper class="com.leige.impl.userimpl"/> </mappers>
使用xml需要再配置一个mappen.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"> <!-- 建立表与类的映射关系有两种:一个是xml(倾向),另一个是注解 --> <mapper namespace="com.leige.entity.usermappen"> <!-- 查询全部 --> <select id="selectall" resulttype="user"> select * from user </select> <!-- 增加用户 --> <update id="insertuser" parametertype="user"> insert into user values (null,#{name},#{password},#{age}) </update> </mapper> 使用注解则用接口 public interface userimpl { //更新用户,更新用@update,查询用@select,插入用@insert,删除使用@delete这些注解 @update("update user set name=#{name} where id=#{id}") public void updateuser(user user); } demo测试代码 public class demo { static inputstream in = null; static sqlsessionfactory ssf = null; static{ in = demo.class.getclassloader().getresourceasstream("config.xml"); ssf = new sqlsessionfactorybuilder().build(in); } public static void main(string[] args) { //selectall(); //insertuser(); updateuser(); } //查询全部 public static void selectall(){ sqlsession session = ssf.opensession(); list<user> list = session.selectlist("com.leige.entity.usermappen.selectall"); system.out.println(list); session.close(); } //增加用户 public static void insertuser(){ sqlsession session = ssf.opensession(); user user = new user(); user.setname("小霸王"); user.setpassword("aaaaa"); user.setage(25); int num = session.insert("com.leige.entity.usermappen.insertuser",user); session.commit(); system.out.println(num); session.close(); } //修改用户信息 public static void updateuser(){ sqlsession session = ssf.opensession(); userimpl userimpl = session.getmapper(userimpl.class); user user = new user(); user.setname("我是大头鬼"); user.setpassword("aaaaa"); user.setage(25); user.setid(79); userimpl.updateuser(user); session.commit(); session.close(); } } </configuration>
以上所述是小编给大家介绍的mybatis开发环境搭建实现数据的增删改查功能,希望对大家有所帮助