mybatis xml 方式的基本用法
一个简单的权限控制请求
创建数据库表语句
create table sys_user(id bigint not null auto_increment,
user_name varchar(50),
user_password varchar(50),
user_email varchar(50),
user_info varchar(50),
head_img blob,
create_time datetime,
primary key(id)
);
create table sys_role(
id bigint not null auto_increment,
role_name varchar(50),
enabled int,
create_by bigint,
create_time datetime,
primary key (id)
);
create table sys_privilege(
id bigint not null auto_increment,
privilege_name varchar(50),
privilege_url varchar(200),
primary key (id));
create table sys_role_privilege(
role_id bigint,
privilege_id bigint);
create table sys_role_role(
user_id bigint,
role_id bigint
);
//test
insert into sys_user values("1",
"admin",
"123456",
"[email protected]",
"manager",
null,
"2018-05-12 23:00:00");
insert into sys_user values("1",
"admin1",
"1234561",
"[email protected]",
"manager1",
null,
"2018-05-12 23:00:00");
insert into sys_role values(
"2",
"manager2",
"2",
"2",
"2018-05-12 23:00:00"
);
insert into sys_user_role values("1","1");
insert into sys_user_role values("2","2");
insert into sys_privilege values ("1",
"用户管理",
"/users");
insert into sys_privilege values ("2",
"用户",
"/roles");
insert into sys_role_privilege values ("1","3");
insert into sys_role_privilege values ("1","1");
public class SysPrivilege {
private long id;
private String privilegeName;
private String privilegeUrl;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPrivilegeName() {
return privilegeName;
}
public void setPrivilegeName(String privilegeName) {
this.privilegeName = privilegeName;
}
public String getPrivilegeUrl() {
return privilegeUrl;
}
public void setPrivilegeUrl(String privilegeUrl) {
this.privilegeUrl = privilegeUrl;
}
public class SysRole {
private long id;
private String roleName;
private int enabled;
private long createBy;
private Date createTime;
private SysUser user;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public int getEnabled() {
return enabled;
}
public void setEnabled(int enabled) {
this.enabled = enabled;
}
public long getCreateBy() {
return createBy;
}
public void setCreateBy(long createBy) {
this.createBy = createBy;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public SysUser getUser() {
return user;
}
public void setUser(SysUser user) {
this.user = user;
}
@Override
public String toString() {
return "SysRole [id=" + id + ", roleName=" + roleName + ", enabled=" + enabled + ", createBy=" + createBy
+ ", createTime=" + createTime + ", user=" + user + "]";
}
}
public class SysRolePrivilege {
private long roleId;
private long privilegeId;
public long getRoleId() {
return roleId;
}
public void setRoleId(long roleId) {
this.roleId = roleId;
}
public long getPrivilegeId() {
return privilegeId;
}
public void setPrivilegeId(long privilegeId) {
this.privilegeId = privilegeId;
}
}
public class SysUser {
private long id;
private String userName;
private String userPassword;
private String userEmail;
private String userInfo;
private byte[] headImg;
private Date createTime;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserInfo() {
return userInfo;
}
public void setUserInfo(String userInfo) {
this.userInfo = userInfo;
}
public byte[] getHeadImg() {
return headImg;
}
public void setHeadImg(byte[] headImg) {
this.headImg = headImg;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "SysUser [id=" + id + ", userName=" + userName + ", userPassword=" + userPassword + ", userEmail="
+ userEmail + ", userInfo=" + userInfo + ", headImg=" + Arrays.toString(headImg) + "]";
}
}
public class SysUserRole {
private long userId;
private long roleId;
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public long getRoleId() {
return roleId;
}
public void setRoleId(long roleId) {
this.roleId = roleId;
}
}
上面是实体类,接口
public interface SysUserMapper {
SysUser selectById(long id);
List selectAll();
List selectRoleByid(long usid);
int insert(SysUser role);
int insert1(SysUser role);
int deleteById(long id);
int updateById(SysUser user);
}
insert into sys_user values(#{id},#{userName},
#{userPassword},
#{userEmail},
#{userInfo},
#{headImg , jdbcType=BLOB}, #{createTime , jdbcType=TIMESTAMP})
insert into sys_user (user_name ,user_password,user_email,user_info,head_img,create_time) values(#{userName},
#{userPassword},
#{userEmail},
#{userInfo},
#{headImg , jdbcType=BLOB}, #{createTime , jdbcType=TIMESTAMP})
delete from sys_user where id=#{id}
update sys_user
set user_name=#{userName},
user_password=#{userPassword},
user_email=#{userEmail},
user_info=#{userInfo},
head_img =#{headImg,jdbcType=BLOB},
create_time=#{createTime,jdbcType=TIMESTAMP}
where id= #{id}
package ibatis.test;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.binding.MapperRegistry;
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.BeforeClass;
import org.junit.Test;
import com.mybatis.demo.Country;
import com.mybatis.demo.SysRole;
import com.mybatis.demo.SysUser;
import com.mybatis.demo.mapper.SysUserMapper;
import junit.framework.Assert;
public class MyCountryTest {
private static SqlSessionFactory sql;
@BeforeClass
public static void init() throws IOException {
// 读取配置文件
Reader read = Resources.getResourceAsReader("mybatis-config.xml");
// 构建sqlsession工厂
sql = new SqlSessionFactoryBuilder().build(read);
read.close();
}
/*
* @Test public void testSelectAll(){ SqlSession openSession =
* sql.openSession(); List selectList =
* openSession.selectList("selectAll"); System.out.println(selectList);
* openSession.close(); }
*/
/**
* Mapped Statements collection does not contain value for
* com.mybatis.demo.UserMapper.selectAll Type interface
* com.mybatis.demo.UserMapper is not known to the MapperRegistry.
* 配置包扫描,所以mapper接口 应该放到扫描包路径下 解决上面异常
* Invalid bound statement (not found):
*
*/
/**
*
*/
@Test
public void testSelectSysUser() {
SqlSession session = sql.openSession();
SysUserMapper mapper = session.getMapper(SysUserMapper.class);
SysUser selcetByid = mapper.selectById(1);
System.out.println(selcetByid);
session.close();
}
@Test
public void testSelectAll(){
SqlSession session =sql.openSession();
SysUserMapper mapper = session.getMapper(SysUserMapper.class);
List selectAll = mapper.selectAll();
System.out.println(selectAll);
session.close();
}
@Test
public void testSelectRoleByid(){
SqlSession session =sql.openSession();
SysUserMapper mapper = session.getMapper(SysUserMapper.class);
List selectRoleByid = mapper.selectRoleByid(1);
System.out.println(selectRoleByid);
session.close();
}
/**
* reflection.ReflectionException
* 单词别写错,反射调用了getset 如果没有这个单词肯定报错
* There is no getter for property named
* sqlsession 不手动执行commit 不会提交数据库
*/
@Test
public void testInsert(){
SqlSession session =sql.openSession();
SysUserMapper mapper = session.getMapper(SysUserMapper.class);
SysUser user = new SysUser();
user.setCreateTime(new Date());
user.setHeadImg(new byte[]{1,2});
user.setUserEmail("[email protected]");
user.setUserInfo("66");
user.setUserName("521");
user.setUserPassword("123");
int insert = mapper.insert(user);
Assert.assertTrue(insert==1);
System.out.println(user.getId());
session.close();
}
/**
* 插入数据
*/
@Test
public void testInsert1(){
SqlSession session =sql.openSession();
SysUserMapper mapper = session.getMapper(SysUserMapper.class);
SysUser user = new SysUser();
user.setCreateTime(new Date());
user.setHeadImg(new byte[]{1,2});
user.setUserEmail("[email protected]");
user.setUserInfo("66");
user.setUserName("521");
user.setUserPassword("123");
int insert = mapper.insert1(user);
Assert.assertTrue(insert==1);
System.out.println(user.getId());
session.commit();
session.close();
}
/**
* 删除
*/
@Test
public void testDelete(){
SqlSession openSession = sql.openSession();
//MapperRegistry mapperRegistry = new MapperRegistry(this); 初始化时放入concurrentHashmap,然后get
SysUserMapper mapper = openSession.getMapper(SysUserMapper.class);
int deleteById = mapper.deleteById(5);
openSession.commit();
openSession.close();
}
/**
* 更新
* 不要按照自己的意愿来写,
*/
@Test
public void testUpdate(){
SqlSession openSession = sql.openSession();
SysUserMapper mapper = openSession.getMapper(SysUserMapper.class);
SysUser selectById = mapper.selectById(1);
selectById.setHeadImg(new byte[]{5,6,7});
mapper.updateById(selectById);
openSession.commit();
openSession.close();
}
}
java的多态性是指main方法在调用属性的时候类可以对这一属性做出反应的情况
//package 1;
class A{
public void test(){
System.out.println("A");
}
}
class D extends A{
public void test(){
S
参考了网上的思路,写了个Java版的:
public class Fibonacci {
final static int[] A={1,1,1,0};
public static void main(String[] args) {
int n=7;
for(int i=0;i<=n;i++){
int f=fibonac
1、查看系统客户端,数据库,连接层的编码
查看方法: http://daizj.iteye.com/blog/2174993
进入mysql,通过如下命令查看数据库编码方式: mysql> show variables like 'character_set_%'; +--------------------------+------
public class MyQueue {
private long[] arr;
private int front;
private int end;
// 有效数据的大小
private int elements;
public MyQueue() {
arr = new long[10];
elements = 0;
front
A binary search tree (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all