UserModule类
package net.wendal.nutzbook.module;
import net.wendal.nutzbook.bean.User;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
@IocBean
@At("/user")
@Ok("json")
@Fail("http:500")
public class UserModule {
@Inject
protected Dao dao;
@At
public int count(){
return dao.count(User.class);
}
}
dao.js文件内容如下:
var ioc = {
dataSource : {
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : "close"
},
fields : {
url : "jdbc:mysql://192.168.8.220:3306/nutzbook",
username : "root",
password : "",
testWhileIdle : true,
validationQuery : "select 1",
maxActive : 100
}
},
dao : {
type : "org.nutz.dao.impl.NutDao",
args : [{refer:"dataSource"}]
}
};
MainModule类代码如下:
package net.wendal.nutzbook;
import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.SetupBy;
import org.nutz.mvc.ioc.provider.ComboIocProvider;
@SetupBy(value=MainSetup.class)
@IocBy(type=ComboIocProvider.class, args={"*js","ioc/","*anno","net.wendal.nutzbook","*tx"})
@Modules(scanPackage=true)
public class MainModule {
}
MainSetup类代码如下:
package net.wendal.nutzbook;
import java.util.Date;
import net.wendal.nutzbook.bean.User;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.Ioc;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;
public class MainSetup implements Setup{
@Override
public void destroy(NutConfig config) {
// TODO Auto-generated method stub
}
@Override
public void init(NutConfig config) {
// TODO Auto-generated method stub
Ioc ioc = config.getIoc();
Dao dao = ioc.get(Dao.class);
Daos.createTablesInPackage(dao, "net.wendal.nutzbook", false);
//初始化默认根用户
if (dao.count(User.class)==0){
User user = new User();
user.setName("admin");
user.setPassword("123456");
user.setCreateTime(new Date());
user.setUpdateTime(new Date());
dao.insert(user);
}
}
}
User类的代码如下:
package net.wendal.nutzbook.bean;
import java.util.Date;
import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Table;
@Table("t_user")
public class User {
@Id
private int id;
@Name
@Column
private String name;
@Column("passwd")
private String password;
@Column
private String salt;
@Column("ct")
private Date createTime;
@Column("ut")
private Date updateTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>nutzbook</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>nutz</filter-name>
<filter-class>org.nutz.mvc.NutFilter</filter-class>
<init-param>
<param-name>modules</param-name>
<param-value>net.wendal.nutzbook.MainModule</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>nutz</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
jar包已加入classpath,大牛们,我是跟着一步步过来的,初始化的时候能插入admin用户记录到t_user表(表服务器启动的时候自动生成了)。