标签:
<?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="mybatis.userMapper">
<!-- <select id="getid" parameterType="int"
resultType="mybatis.User">
select * from users where id=#{id}
</select> -->
<insert id="insertUser" parameterType="mybatis.User">
insert into users(name, age) values(#{name}, #{age});
</insert>
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
<update id="updateUser" parameterType="mybatis.User">
update users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="selectUser" parameterType="int" resultType="mybatis.User">
select * from users where id=#{id}
</select>
<select id="selectAllUsers" resultType="mybatis.User">
select * from users
</select>
</mapper>
<?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://172.171.2.161:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root123" />
</dataSource>
</environment>
</environments>
<mappers>
<!--xml方式 -->
<mapper resource="mybatis/userMapper.xml"/>
</mappers>
</configuration>
package mybatis;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtil
{
//返回session
public static SqlSession openSqlSession()
{
String resource = "conf.xml";
InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
//貌似可有可无
session.commit();
return session;
}
}
插入用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* xml方式测试
*/
String statement = "mybatis.userMapper.insertUser";
int tuser = MybatisUtil.openSqlSession().insert(statement, new User(3, "rose", 3));
MybatisUtil.openSqlSession().close();
System.out.println(tuser);
}
}
更新用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* xml方式测试
*/
String statement = "mybatis.userMapper.updateUser";
int tuser = MybatisUtil.openSqlSession().update(statement, new User(3, "rose", 3));
MybatisUtil.openSqlSession().close();
System.out.println(tuser);
}
}
删除用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* xml方式测试
*/
String statement = "mybatis.userMapper.deleteUser";
int tuser = MybatisUtil.openSqlSession().delete(statement,2));
MybatisUtil.openSqlSession().close();
System.out.println(tuser);
}
}
查询全部用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* xml方式测试
*/
String statement = "mybatis.userMapper.selectAllUsers";
List<User> tuser = MybatisUtil.openSqlSession().selectList(statement);
MybatisUtil.openSqlSession().close();
System.out.println(tuser);
}
}
package mybatis;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
public interface UserMapperDe
{
@Insert("insert into users(name, age) values(#{name}, #{age})")
public int insertUser(User user);
@Delete("delete from users where id=#{id}")
public int deleteUserById(int id);
@Update("update users set name=#{name},age=#{age} where id=#{id}")
public int updateUser(User user);
@Select("select * from users where id=#{id}")
public User getUserById(int id);
@Select("select * from users")
public List<User> getAllUser();
}
<?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://172.171.2.161:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root123" />
</dataSource>
</environment>
</environments>
<mappers>
<!--xml方式 -->
<mapper resource="mybatis/userMapper.xml"/>
<!--注解方式 -->
<mapper class="mybatis.UserMapperDe"/>
</mappers>
</configuration>
插入用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* 注解方式测试
*/
UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
int inuser = mapper.insertUser(new User(-1, "clock", 1));
MybatisUtil.openSqlSession().close();
System.out.println(inuser);
}
}
更新用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* 注解方式测试
*/
UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
int inuser = mapper.updateUser(new User(2, "efpd", 2));
MybatisUtil.openSqlSession().close();
System.out.println(inuser);
}
}
删除用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* 注解方式测试
*/
UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
int inuser = mapper.deleteUserById(2);
MybatisUtil.openSqlSession().close();
System.out.println(inuser);
}
}
查询全部用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* 注解方式测试
*/
UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
List<User> inuser = mapper.getAllUser();
MybatisUtil.openSqlSession().close();
System.out.println(inuser);
}
}
标签:
原文地址:http://www.cnblogs.com/angerze/p/c9214f160f7301c79dbf80af58e6a8bc.html